Export AD Computer List

For creating a few group policy preferences, I needed to come up  with a list of all the OU’s (and a simple export from the ADUC gui doesn’t cut it).  I also wanted a rough count of the pc’s in each ou for various other purposes.

This will be used with another list that contains the ou name and the names of several AD groups. I’ll be manually selecting from those AD groups which ones will be put into policy as Admins on the pc.  Here’s what I ended up with:

Get-ADComputer -Filter * | ForEach-Object{
$dn = $_.DistinguishedName.Split(‘,’)
New-Object PSObject -Property @{
Name = $dn[0] -replace ‘^cn=’
ParentContainer = $dn[$dn.length..1] -notlike ‘dc=*’ -join ‘,’
}
} | Out-File c:\temp\OUExport.txt

I then imported into excel as a fixed width delimiter, enabled filtering, and then sorted.

It would be nice to have a list as well that strips all the OU’s except the immediate prior (ie, ou=master,ou=lab,ou=simpsons -> ou=simpsons) but I’ll have to google for that later..

 

Advertisements