Home » PowerShell » Get-AdUser from OU

Get-AdUser from OU

The Get-AdUser cmdlet in PowerShell is used to list all users in ou. The Get-Aduser SearchBase parameter is used to searchbase single ou or multiple ou in the Active Directory and get aduser from ou.

Get-AdUser from OU
Get-AdUser from OU

In this article, we will discuss how to get all users from ou, using the Get-AdUser Searchbase parameter to get ad users from specific ou and export ad users from specific ou to CSV file in PowerShell.

Get-AdUser from OU

Using the Get-AdUser Active Directory cmdlet, we can get ad users from ou or sub ou.

# Specify the OU Path
$OUPath = 'OU=HR,DC=SHELLPRO,DC=LOCAL'

# List all users from OU path
Get-ADUser -Filter * -SearchBase $OUPath | Select-Object GivenName,SamAccountName,DistinguishedName,UserPrincipalName

In the above PowerShell script, we have specified the organizational unit (OU) path.

The Get-AdUser uses the SearchBase parameter to search for ad users from the specified OU path.

The Select-Object cmdlet is used to get-aduser properties from the specific OU.

It returns the list of all users from a specific OU path.

Get-AdUser by OU
Get-AdUser by OU

Get AdUser in OU and Export to CSV

Use the Get-AdUser cmdlet in PowerShell to get adusers in OU and export ad users from specific OU to a CSV file using the Export-CSV cmdlet.

The following command gets aduser by ou and export aduser to CSV.

$OUPath = 'OU=SALES,DC=SHELLPRO,DC=LOCAL'

Get-ADUser -Filter * -SearchBase $OUPath | Select-Object GivenName,SamAccountName,DistinguishedName,UserPrincipalName | Export-Csv -Path C:\PowerShell\aduser-in-ou.csv -NoTypeInformation

In the above PowerShell script, the $OUPath variable contains ou location.

Get-AdUser uses Filter and SearchBase parameters to get aduser in OU. Use the Select-Object to select aduser properties like samaccountname, userprincipalname.

The Export-CSV cmdlet in PowerShell export ad users from ou to the CSV file.

Get AdUser from Sub OU

Organizational Unit in the Active Directory contains users, computers, groups, and nested OU. Using the Get-AdUser SearchScope parameter, we can get adusers from ou and sub ou.

In the following command, it gets all users from ou and sub ou.

$OUPath = 'OU=SHELLUSERS,DC=SHELLPRO,DC=LOCAL'

Get-ADUser -Filter * -SearchBase $OUPath -SearchScope Subtree

In the above PowerShell script, we have specified the OU path. SHELLUSERS Ou contains users, computers, and nested OUs.

Using the Get-AdUser SearchScope parameter, it gets all users from OU and sub ou.

The output of the above command is:

List Users from OU and Sub OU
List Users from OU and Sub OU

Cool Tip: How to find disabled adusers in OU using PowerShell!

Conclusion

I hope the above article on how to get aduser from ou and sub ou is helpful to you.

Get-AdUser cmdlet in the Active Directory module is used to list all users from specific ou. Use the SearchBase parameter and SearchScope parameter to limit the search for ad users in the Active Directory.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.