Testing Organizational Unit

Provided you have installed the free Microsoft RSAT tools, here is a simple way to check whether an OU exists:

$OUPath = 'OU=TestOU,DC=train,DC=powershell,DC=local'
$exists = $(try { Get-ADOrganizationalUnit -Identity $OUPath -ErrorAction Ignore } catch{}) -ne $null
"$OUPath : $exists"

$exists will be $true or $false, indicating whether the OU was found. Note the use of try/catch error handling: Get-ADOrganizationalUnit can raise terminating errors when the specified OU does not exist, so try/catch is needed to capture these exceptions.

Twitter This Tip! ReTweet this Tip!

  • Using Get-ADOrganizationalUnit's Filter parameter, you'll get a $null response if the OU doesn't exist.

    $OUPath = 'OU=TestOU,DC=train,DC=powershell,DC=local'

    If(!(Get-ADOrganizationalUnit -Filter "DistinguishedName -eq '$OUPath'")) {

       Write-Warning "Organizational Unit $OUPath does not exist."



  • also, using ADSI type accelerator will return nothing if it isn't there.  therefore the following will return true only if it exists (nothing if it doesn't )

    if(([ADSI]"LDAP://OU=TestOU,DC=train,DC=powershell,DC=local").schemaclassname -eq "organizationalUnit"){$true}