Technology Solutions for Everyday Folks
Road sign reading "Export"

Powershell Exports AD Computers to CSV

A couple months ago I wrote about using Powershell to find and export AD records for the purposes of our Windows 7 End-of-Life project. This post is effectively a second in a series of 'exporting computer records from AD with Powershell' if you will.

A few weeks ago I encountered a situation where I needed to grab a list of computer names and IP addresses for a specific physical space. It just so happens that this space matches up with a specific Organizational Unit (OU) in AD. Since we're all remote and asking our network guy to run a report is more complicated than normal, I decided to use AD information to grab what I needed, and do so quickly.

Active Directory, or MEMCM?

Ideally, I'd have preferred to grab this info directly from the MEMCM (f/k/a SCCM) console, and if I needed MAC addresses I'd totally have done so, but since we don't have good IP address data in that environment it was necessary to pull from AD where the data is in better shape.

Get-AdComputer -Filter * -Property * 
  -SearchBase 'OU=PhysicalSpace,OU=LabMachines,OU=Computers,OU=Unit,DC=fabrikam,DC=com' | 
  Select-Object Name, IPv4Address | 
  Export-CSV .\PhysicalSpace-ADComputers.csv -NoTypeInformation -Encoding UTF8

There's one bit of 'magic' in how this particular one behaves: the use of the -SearchBase parameter. Set this parameter to the distinguished name (DN) of the OU in question. To quickly find the DN of the OU, if you happen to know one of the computer names within it's simple with the following:

Get-ADComputer -Filter 'Name -like "COMPUTERNAME"' -Property DistinguishedName | Select-Object DistinguishedName

You'll want to make sure to remove the common name (CN) record at the beginning of the result string (it looks like CN=COMPUTERNAME, before the OU= business shows up) when copy/pasting into the actual export query. Otherwise your results will...probably not be what you desire.

At the end of the short exercise, I ran the export script and voila, there it was in CSV—just the exact bits I needed to pass along for the project. I made a gist of the export script above if you're interested in using.

Keep scripting!

Headline image via commercial language training