List Users in OU using Powershell
An application developer in my company needed to list users in OU with some specific properties. He wanted to get OU members and import in his application to perform some tasks. There are multiple ways to achieve this. The simplest way is to run Get-Aduser in Powershell on a domain controller.
Get-ADUser -SearchBase “OU=OUNAME,dc=Domain,dc=com” -Filter *
Here SearchBase is used to specify the OU name and path while Filter is used to filter out any results if you need, * is used to include everything.
If you have lots of users in the OU, then you may have to add -ResultSetSize to the above command, “$Null” gives unlimited results.
Get-ADUser -SearchBase “OU=OUName,dc=Domain,dc=com” -Filter * -ResultSetSize $Null
Next, you can specify the properties you want to display
Get-ADUser -SearchBase “OU=OUName,dc=Domain,dc=com” -Filter * -ResultSetSize $Null | select Name, SamAccountName
Finally, if you want to export the result, you can use Export-csv as follows:
Get-ADUser -SearchBase “OU=OUName,dc=Domain,dc=com” -Filter * -ResultSetSize $Null | select Name, SamAccountName | export-csv D:\UserInOUList.csv
You can check my related article that i wrote about listing Users by Mailbox Size in exchange.