Reputation: 85
I am trying to exclude an OU (sub-OU) from a search with PowerShell.
This is my code:
Get-ADOrganizationalUnit -filter * -SearchBase 'OU=test,DC=test,DC=com' |
foreach {
$users=Get-ADUser -filter * -searchbase $_.distinguishedname -ResultPageSize 2000 -resultSetSize 500 -searchscope Onelevel | where-object enabled -eq true
$total=($users | measure-object).count
New-Object psobject -Property @{
OU=$_.Name;
A=$Total
}
}
I'm a PowerShell beginner. Can you please help me to exclude an OU from the search results?
Thanks
Upvotes: 0
Views: 1570
Reputation: 3918
By adding an if statement:
Get-ADOrganizationalUnit -filter * -SearchBase 'OU=test,DC=test,DC=com' | foreach {
if($_.distinguishedname -ne "OU=not,OU=that,OU=orgUnit,OU=test,DC=test,DC=com"){
$users=Get-ADUser -filter * -searchbase $_.distinguishedname -ResultPageSize 2000 -resultSetSize 500 -searchscope Onelevel | where-object enabled -eq true
$total=($users | measure-object).count
New-Object psobject -Property @{
OU=$_.Name;
A=$Total
}
}
}
Upvotes: 1