List All Users in an OU

powershell1.jpg

Overview:

There may be a time when you need to list all the users of a specific OU, not just the entire domain.  An example, is if your Organizational Units were broken down in departments and you wanted to compare that department to an active employee roster for instance.  Other times, you may seperate your OU's into one that contains your company accounts vs. service accounts.

Knowledge:

For this case, you can use the Get-ADuser commandlet in order to do so.  Below is a screenshot of the default properties shown, and a second showing the extended properties.

Extended Properties:

Example Command:

This command will search the OU Company in the Domain Example.com, It will also select just the Surname, and Given Name sorting by the Surname.

Get-ADUser -Filter * -SearchBase "ou=company,dc=example,dc=com" | Select-Object Surname, GivenName | Sort-Object Surname

If you wanted to PIPE it out to a CSV file so you can open in Excel you can do that by using the Export-Csv command:

Get-ADUser -Filter * -SearchBase "ou=company,dc=example,dc=com" | Select-Object Surname, GivenName | Sort-Object Surname | Export-Csv c:\temp\AllUsers.csv
References:

TechNet Wiki Get-ADUser