Skip to main content

List the PowerShell Users in an Organizational Unit

Problem

You want to list all the users in an OU.

Solution

To list the users in an OU, use the [adsi] type shortcut to bind to the OU in Active Directory. Create a new System.DirectoryServices.DirectorySearcher for that OU, and then set its Filter property to (objectClass=User). Finally, call the searcher’s FindAll() method to perform the search.

$sales = [adsi] "LDAP://localhost:389/ou=Sales,dc=Fabrikam,dc=COM"

$searcher = NewObject System.DirectoryServices.DirectorySearcher $sales $searcher.Filter = '(objectClass=User)' $searcher.FindOne()

Discussion

The solution lists all users in the Sales OU. It does this through the System. DirectoryServices.DirectorySearcher class from the .NET Framework, which lets you query Active Directory. The Filter property specifies an LDAP filter string.

By default, a DirectorySearcher searches the given container and all containers below it. Set the SearchScope property to change this behavior. A value of Base searches only the current container, while a value

of OneLevel searches only the immediate children.

If you need dedicated infrastructure for listing users within an Organizational Unit using Windows PowerShell, consider a Netherlands Dedicated Server for greater control over Active Directory management, user administration, PowerShell automation, system configuration, and Windows-based application workloads.

Help Category