Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a question on how to exclude a specific OU when running this PS statement. This gives me all the servers in the Domain. I like to excluded the OU called


Tried some filtering but no luck.

$ObjFilter = "(operatingsystem=*SERVER*)" 
$objSearch = New-Object System.DirectoryServices.DirectorySearcher 
$objSearch.PageSize = 15000 
$objSearch.Filter = $ObjFilter 
$objSearch.SearchRoot = "LDAP://dc=ABCCORP,dc=com"
$AllObj = $objSearch.FindAll() | Select-Object @{n='Name';e={$['name']}} 
share|improve this question
up vote 2 down vote accepted

You can pipe the search result to Where-Object and filter based on the LDAP Path.


$AllObj = $objSearch.FindAll() | Select-Object @{n='Name';e={$['name']}}


$AllObj = $objSearch.FindAll() | Where {-not ($_.Path -match 'OU=RETIRED,OU=SERVER,DC=ABCCORP,dc=com')} | Select-Object @{n='Name';e={$['name']}}
share|improve this answer
+1 Using the path is even more succinct than my suggestion. – jscott Aug 17 '12 at 23:34
Thanks to all - both of these statements gives me what I needed $AllObj = $objSearch.FindAll()| Where-Object { $_.Properties['distinguishedname'] -notlike "*,OU=RETIRED,OU=SERVER,DC=ABCCORP,dc=com"} | Select-Object @{n='Name';e={$['name']}} $AllObj = $objSearch.FindAll()| Where {-not ($_.Path -match 'OU=RETIRED,OU=SERVER,DC=ABCCORP,dc=com')} | Select-Object @{n='Name';e={$['name']}} – CWL Aug 19 '12 at 2:24
@CWL Glad Sean's answer worked for you. Be sure to mark it as "accepted" when you are able. This will help others, with similar issues, find your question easier. Also note that an explicit "thanks" on SE works a bit different than other forums. :) – jscott Aug 19 '12 at 11:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.