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))))
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.
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?
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)"
}