Friday, 20 September 2019

Find and Export Manager of All Office 365 Users using Powershell

In this post, I am going to share powershell script to find manager info of all Office 365 users and export the details to CSV file. We can use the Get-AzureADUser command to get user details, but this command does not include manager based details, so we have to use the Get-AzureADUserManager cmdlet to get a user's manager info.

Before proceeding, install 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 "username@domain.com"

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 command and pipe the result to Get-AzureADUserManager command. The below commands fetch all users and their manager details (ex: name and email address) and store the detail in the $Result array object.
$Result = @()
$AllUsers= Get-AzureADUser -All $true | Select-Object -Property Displayname,UserPrincipalName
$TotalUsers = $AllUsers.Count
$i = 1 
$AllUsers | ForEach-Object {
$User = $_
Write-Progress -Activity "Processing $($_.Displayname)" -Status "$i out of $TotalUsers completed"
$managerObj = Get-AzureADUserManager -ObjectId $User.UserPrincipalName
$Result += New-Object PSObject -property @{ 
UserName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
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 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 CSV file

You can easily export the result to CSV file using the Export-CSV cmdlet.
$Result | Select UserName, UserPrincipalName, ManagerName,ManagerMail  |
Export-CSV "C:\\O365UsersManagerInfo.CSV" -NoTypeInformation -Encoding UTF8

Find manager info for multiple users from CSV file

In some scenarios, we may need to find manager name for particular set of users. In this case, we can store the user ids in CSV file and import csv in powershell using Import-Csv cmdlet. Consider the CSV file "O365Users.csv" which contains username (or upn) of users with the column header UserPrincipalName.
$Result = @()
Import-Csv 'C:\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:\\O365UsersManagerInfo.CSV" -NoTypeInformation -Encoding UTF8

Advertisements
Advertisements

No comments:

Post a Comment