PowerShell - Limit the search to only one OU

Posted by NirPes on Server Fault See other posts from Server Fault or by NirPes
Published on 2014-06-04T15:10:52Z Indexed on 2014/06/04 15:28 UTC
Read the original article Hit count: 237

Ive got this cmdlet and I'd like to limit the results to only one OU:

Get-ADUser -Filter  {(Enabled -eq $false)} | ? { ($_.distinguishedname -notlike '*Disabled Users*') } 

Now Ive tried to use

-searchbase "ou=FirstOU,dc=domain,dc=com"

But if I use -SearchBase I get this error:

Where-Object : A parameter cannot be found that matches parameter name 'searchb
ase'.
At line:1 char:114
+ Get-ADUser -Filter  {(Enabled -eq $false)} | ? { ($_.distinguishedname -notli
ke '*Disabled Users*') } -searchbase <<<<  "ou=FirstOU,dc=domain,dc=com"
    + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBi
   ndingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
   ands.WhereObjectCommand

What Im trying to do is to get all the disabled users from a specific OU, BUT, there is an OU INSIDE that FirstOU that I want to exclude: the "Disabled Users" OU.

as you might have guessed I want to find disabled users in a specific OU that are not in the "Disabled Users" OU inside that OU.

my structure:

Forest
   FirstOU
      Users,groups,etc...
      Disabled Users OU

© Server Fault or respective owner

Related posts about active-directory

Related posts about powershell