Skip navigation
Check if an OU exists using PowerShell

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.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish