Today, I received a request to find the manager for a whole bunch of users. This was a list of names (not UserId’s) in a Excel worksheet.
It is not actually that complex to do it
- Locate the AD user based on the name
- Check the Manager property
- Lookup AD again for Manager to get the name
Prerequisite 1: Active Directory Module:
Before we begin, if you are running Windows 7 or above and if you do not have the PowerShell Active Directory module installed, please do so first by downloading and installing “Remote Server Administration Tools”. I personally cannot live without this module.
The PowerShell commands that will be used in this post are from the above module!
Prerequisite 2: Flexible AD account lookup by name:
Please checkout my other post on how to correlate a bunch of names to their corresponding AD accounts. We need that to do what we need to do
Step by Step:
As I said, I received an Excel workbook.
- From the workbook, I simply copied the “EmployeeName” column into a text file
- Saved the text file
- Ran the below routine supplying the file location (line #1 below)
- Pasted the output back into the workbook and returned it
Code:
[string] $inputFile = 'c:\Temp\ADManagerLookup\Users.txt' #Change this! #-------------------- #Read the input file #-------------------- $list = Get-Content -LiteralPath $inputFile #-------------------- #Loop through the list #-------------------- foreach($user in $list) { $adUser = $null #-------------------- #Get the AD account #-------------------- $adUser = Get-ADUserNames -UserNamesString $user $object = New-Object –TypeName PSObject $object | Add-Member –MemberType NoteProperty –Name Employee –Value $user $object | Add-Member –MemberType NoteProperty –Name Manager –Value '' $object | Add-Member –MemberType NoteProperty –Name Status –Value $adUser.Status $object | Add-Member –MemberType NoteProperty –Name StatusMessage –Value $adUser.StatusMessage if ($adUser -ne $null) { #-------------------- #Get the Manager #-------------------- $manager = $adUser.ADUser.Manager if ($manager -ne $null) { #-------------------- #Lookup the manager account #-------------------- $object.Manager = (Get-ADUser $manager).Name } } #Write the output Write-Output $object }
The output looks like below:
Export output to CSV
You can even save the output of the script and export to CSV!
Let us say that you saved the above script to the location “c:\Temp\ADManagerLookup\ManagerLookup.ps1”, the below script should help you with the CSV export
[string] $inputFile = 'c:\Temp\ADManagerLookup\Users.txt' #Change this! [string] $outputFile = 'c:\Temp\ADManagerLookup\UsersAndManagers.txt' #Change this! #Run our saved script to get the output $results = c:\Temp\ADManagerLookup\ManagerLookup.ps1 #Export the output to CSV $results | Export-Csv -LiteralPath $outputFile -NoTypeInformation -Force
Conclusion:
Your AD setup may not be exactly like mine but it should be simple enough to make the minor changes to get not just the manager but also any other attribute associated with the user.
Help someone out with their “Manager Lookup” and I am sure you will look like a hero! You would have probably saved them hours or even days depending on the size of the list!
Also, checkout my related post about moving users around AD groups easily (or simply search for “PowerShell AD” on my blog)
One thought on “PowerShell & Active Directory: Quickly Find The Manager For A Bunch Of Users”