title: Powershell checking if OU exist
body: <p>I'm trying to check if an OU exist before creating it. My problem is that I have 2 mother OU "USER BY SITE" and "GROUP BY SITE", and I need to have the exact same OU in those 2, 1 for storing users, the other for storing groups.</p>
  
  <p>So far I used this function :</p>
  
  <pre><code>function CheckOUExist
  {
      param($OUToSeek)
  
      $LDAPPath = "LDAP://dc=Domain,dc=local"
  
      $seek = [System.DirectoryServices.DirectorySearcher]$LDAPPath
      $seek.Filter = “(&amp;(name=$OUToSeek)(objectCategory=organizationalunit))”
      $Result = $seek.FindOne()
  
      return $Result
  }
  </code></pre>
  
  <p>There is my problem, I always get the OU existing in "GROUP BY SITE" even if $LDAPPath = "OU=USERS BY SITE,DC=Domain,DC=local". Am I missing something there? Is there a way to for the [System.DirectoryServices.DirectorySearcher] to work only in the OU I gived in the $LDAPPath?</p>
  
  <p>If you need more accurate detail, I'll gladly provide them.</p>
  
  <p>Thank you in advance.</p>