In this post, I am going to share a PowerShell script to find the manager info of all Office 365 users and export the details to a CSV file. We can use the Get-AzureADUser command to get user details, but this command does not include the manager details, so we have to use the Get-AzureADUserManager cmdlet to get a user’s manager info.
Before you start, install the Azure AD PowerShell V2 module and run the below command to connect Azure AD Powershell:
Connect-AzureAD
Run the following command to retrieve manager information for a single user account.
Get-AzureADUserManager -ObjectId "[email protected]"
Get Manager of All Azure AD Users
To get the manager detail of all users, first, we have to get all Office 365 users using the Get-AzureADUser cmdlet and iterate the users one by one to get manager info using the Get-AzureADUserManager cmdlet.
The below commands fetch all users and their Organization details, such as Manager name, Company name, Job title, and Department. Finally, store the detail in the $Result array object.
$Result = @() $AllUsers= Get-AzureADUser -All $true | Select-Object -Property Displayname,UserPrincipalName,CompanyName,JobTitle,Department,AccountEnabled $TotalUsers = $AllUsers.Count $i = 1 $AllUsers | ForEach-Object { $User = $_ Write-Progress -Activity "Fetching manager of $($_.Displayname)" -Status "$i out of $TotalUsers users completed" $managerObj = Get-AzureADUserManager -ObjectId $User.UserPrincipalName $Result += New-Object PSObject -property $([ordered]@{ UserName = $User.DisplayName UserPrincipalName = $User.UserPrincipalName CompanyName = $User.CompanyName JobTitle = $User.JobTitle Department = $User.Department AccountStatus = if ($User.AccountEnabled -eq $true) { "Enabled" } else { "Disabled" } ManagerName = if ($managerObj -ne $null) { $managerObj.DisplayName } else { $null } ManagerMail = if ($managerObj -ne $null) { $managerObj.Mail } else { $null } }) $i++ }
After the successful run of the above commands, you can run the below command to list all office 365 users with their manager info.
$Result | Select UserName, ManagerName, ManagerMail
List All Office 365 Users without Manager:
You can just filter the $Result array with the Where-Object filter to list users with no manager.
$Result | Where-Object { $_.ManagerName -eq $null } | Select UserName, UserPrincipalName
Alternatively, you can list users only who have a manager.
$Result | Where-Object { $_.ManagerName -ne $null } | Select UserName, ManagerName
Export the Result to a CSV file
You can easily export the result to a CSV file using the Export-CSV cmdlet.
$Result | Export-CSV "C:\Temp\M365UsersManagerInfo.CSV" -NoTypeInformation -Encoding UTF8
Find manager info for multiple users from CSV file
Sometimes, we may need to find the manager name for a particular set of users. In this case, we can store the user ids in a CSV file and import CSV in PowerShell using the Import-CSV cmdlet. Consider the CSV file “O365Users.csv” which contains the UPN of the required users with the column header UserPrincipalName.
$Result = @() Import-Csv "C:\Temp\O365Users.csv" | ForEach-Object { $managerObj = Get-AzureADUserManager -ObjectId $_."UserPrincipalName" $Result += New-Object PSObject -property @{ UserName = $_."UserPrincipalName" ManagerName = if ($managerObj -ne $null) { $managerObj.DisplayName } else { $null } ManagerMail = if ($managerObj -ne $null) { $managerObj.Mail } else { $null } } } $Result | Select UserName, ManagerName,ManagerMail | Export-CSV "C:\Temp\M365UsersManagerInfo.CSV" -NoTypeInformation -Encoding UTF8
That was really helpful. thanks.
I’m getting the below error when trying to export:
Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
At line:1 char:11
+ $Result | Export-CSV “C:\Temp\M365UsersManagerInfo.CSV” -NoTypeInform …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
mmand
Not sure what i’m doing wrong?
Ensure that your CSV file (“C:\Temp\O365Users.csv”) includes the column header “UserPrincipalName”?.
Hi Morgan, I am having the exact same issue as Darren. Reading your suggestion does not compute. Can you explain exactly what you mean by:
“Ensure that your CSV file (“C:\Temp\O365Users.csv”) includes the column header “UserPrincipalName”?.”
Can you just run the following command and it returns the result?
$Result
Also, can you post the error message you are facing?