Skip to content

/flags/active-directory/get-aduser-searchbase-searchscope

Get-ADUser -SearchBase and -SearchScope: search one OU, with or without sub-OUs

The commands on this page have not been executed by our verification runner yet.updated
tool
Active Directory PowerShell
command
Get-ADUser
flag
-SearchBase <DN>, -SearchScope Base|OneLevel|Subtree
default
SearchBase: the default naming context of the domain; SearchScope: Subtree

Get-ADUser -Filter * -SearchBase "OU=Sales,DC=contoso,DC=com" returns only the users inside that OU and its child OUs. Add -SearchScope OneLevel to return only the users directly in the OU, without sub-OUs. Without -SearchBase, Get-ADUser searches the whole domain: outside an AD drive, "the default value of this parameter is the default naming context of the target domain."

powershell
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=Sales,DC=contoso,DC=com"
Get-ADUser -Filter * -SearchBase "OU=Sales,DC=contoso,DC=com" -SearchScope OneLevel
Get-ADUser -Filter 'Enabled -eq $false' -SearchBase "OU=Staff,DC=contoso,DC=com" -Properties LastLogonDate

-SearchScope values

From the reference:

ValueSearchesNote
Base or 0only the object named in -SearchBasewith an OU as base, no user is returned
OneLevel or 1the immediate children of the baseworks only when an OU is given as the base
Subtree or 2the base and all objects below itthe behaviour you get by default

Writing the DN

-SearchBase takes a distinguished name, from the most specific part to the domain: OU=Laptops,OU=Computers,OU=Berlin,DC=contoso,DC=com. Rules that trip people up:

  • Use OU= for organizational units and CN= for containers: the built-in Users and Computers folders are containers, so the base is CN=Users,DC=contoso,DC=com.
  • Quote the DN; values with commas inside a name must be escaped with a backslash.
  • An empty string as -SearchBase searches all partitions only when you are connected to a global catalog port; otherwise "an error is thrown".

Pitfalls

  • Wrong DN. A base that does not exist fails with a "directory object not found" style error rather than returning nothing; check it with Get-ADOrganizationalUnit -Identity "OU=...".
  • OneLevel on a user. If the base is a user object, OneLevel returns nothing, as the reference notes.
  • Other domains. The DN must belong to the domain you query; for another domain add -Server with a domain controller of that domain (Get-ADUser -Server).
  • Filters still apply. -SearchBase narrows where, -Filter narrows what (Get-ADUser -Filter); add -Properties for extra attributes (Get-ADUser -Properties).
  • Single user lookups use -Identity; a wrong name fails with Cannot find an object with identity.

FAQ

How do I find the distinguished name of an OU?

Run Get-ADOrganizationalUnit -Filter 'Name -eq "Sales"' | Select-Object DistinguishedName, or copy distinguishedName from the Attribute Editor tab in Active Directory Users and Computers (with Advanced Features enabled).

Does -SearchBase work with -Identity?

No. -SearchBase and -SearchScope belong to the -Filter and -LDAPFilter parameter sets. With -Identity the cmdlet looks up one object anywhere in the domain.

Can I search several OUs at once?

Not in one call. Loop over the OUs: 'OU=Sales,DC=contoso,DC=com','OU=HR,DC=contoso,DC=com' | ForEach-Object { Get-ADUser -Filter * -SearchBase $_ }.

Sources