Link to home
Create AccountLog in
Avatar of crp0499
crp0499Flag for United States of America

asked on

Active Directory query for manager

I have an Active Directory Query written to show me the list of active users.  It's working great.  My syntax is below.  

Now I'm being asked to add the users manager to the query and i'm struggling on how to make that happen.  

Can someone please give me syntax to add the users manager to my query?


(&(&(objectCategory=user)(ultiProEmployeeNumber=*)))(&(&(objectCategory=user)(userAccountControl=512)))(&(&(objectCategory=user)(!(msDS-parentdistname=OU=Other,OU=Information Technology,DC=xyz,DC=local))))

Avatar of Rodney Barnhardt
Rodney Barnhardt
Flag of United States of America image

Is something like this what you are looking for?


# Define the filter criteria using your original query

$filter = "(&(&(objectCategory=user)(ultiProEmployeeNumber=*))(userAccountControl -eq 512)(!(msDS-parentdistname=OU=Other,OU=Information Technology,DC=xyz,DC=local)))"


# Gets the user accounts that match the filter above

$users = Get-ADUser -Filter $filter -Properties manager


# Locate the users managers

foreach ($user in $users) {

    $manager = Get-ADUser -Identity $user.manager -Properties DisplayName

    Write-Host "User: $($user.DisplayName), Manager: $($manager.DisplayName)"

}


Avatar of crp0499

ASKER

I'm always so impressed with people who understand this.  :)  Is this an AD query?  I'll build out a test query and try it out and get back to you.

Avatar of crp0499

ASKER

I went into AD, created a query and pasted this code.  I am getting an error.


(&(&(objectCategory=user)(ultiProEmployeeNumber=*))(userAccountControl -eq 512)(!(msDS-parentdistname=OU=Other,OU=Information Technology,DC=xyz,DC=local)))"$users = Get-ADUser -Filter $filter -Properties manager($user in $users) {$manager = Get-ADUser -Identity $user.manager -Properties DisplayName Write-Host "User: $($user.DisplayName), Manager: $($manager.DisplayName))


Of course, I changed the domain back to my domain.

Can you provide what the error is?

Avatar of crp0499

ASKER

User generated image


ASKER CERTIFIED SOLUTION
Avatar of Rodney Barnhardt
Rodney Barnhardt
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer