Check if an OU exists using PowerShell

Learn how to check if objects exist in AD using PowerShell

John Savill

September 6, 2016

1 Min Read
Check if an OU exists using PowerShell

Q. How can I check if an Organization Unit exists in Active Directory using PowerShell?

A. The easiest way to check for an Organizational Unit is by checking via the ADSI interface and LDAP. Create a $path variable which is the fully distinguished name of the OU then check if it exists as follows:

 [string] $Path = "OU=Labs,OU=NA,DC=savilltech,DC=net"if(![adsi]::Exists("LDAP://$Path")){}

In the example the subsequent code for the IF statement will run if the OU does not exist.

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like