Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

OU=RETIRED,OU=SERVER,DC=ABCCORP,dc-com

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={$_.properties['name']}} 
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

change:

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

to:

$AllObj = $objSearch.FindAll() | Where {-not ($_.Path -match 'OU=RETIRED,OU=SERVER,DC=ABCCORP,dc=com')} | Select-Object @{n='Name';e={$_.properties['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={$_.properties['name']}} $AllObj = $objSearch.FindAll()| Where {-not ($_.Path -match 'OU=RETIRED,OU=SERVER,DC=ABCCORP,dc=com')} | Select-Object @{n='Name';e={$_.properties['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

 
discard

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.