In my helpdesk role, one of the most frustrating tasks is assigning new line managers for an entire team in AD Users and Computers (ADUC) GUI.
Luckily, PowerShell comes to the rescue.
Instead of manually opening each user’s properties and checking or changing their manager one by one, I used the following PowerShell scripts.
Finding Managers in Bulk
Here’s a script that loops through a list of users and retrieves their current manager:
# List of users
$users = @("thor", "bbanner", "nromanoff")
# Loop through each user to get their manager's name
foreach ($user in $users) {
# Get the user details including the Manager property
$userDetails = Get-ADUser -Identity $user -Properties Manager
# Get the manager's Distinguished Name (DN) and resolve it to a readable name
if ($userDetails.Manager) {
$managerDN = $userDetails.Manager
$manager = Get-ADUser -Identity $managerDN
write-host "$user's manager is: $($manager.Name)"
} else {
write-host "$user does not have a manager listed."
}
}
Changing Managers in Bulk
If you need to update the manager for multiple users, this PowerShell script makes it easy:
# List of users
$users = @("thor", "bbanner", "nromanoff")
# Get Nick Fury's Distinguished Name (DN) to assign as the manager
$nickFury = Get-ADUser -Identity "nfury"
$nickFuryDN = $nickFury.DistinguishedName
# Loop through each user to set their manager to Nick Fury
foreach ($user in $users) {
# Set the Manager attribute to Nick Fury's Distinguished Name
Set-ADUser -Identity $user -Manager $nickFuryDN
write-host "$user's manager has been changed to Nick Fury."