Microsoft 365: SharePoint Governance – Get manager of Azure AD user and Export to CSV using PowerShell

.
“When you focus on the good, the good gets better. “
.
Hello Everyone,
Hope you all are doing well.
In this article we are going to discuss how to get the manager of the Azure AD user and export to CSV through PowerShell.
We have good list of articles on SharePoint Online using PowerShell, please have a look.
So without getting late, let’s get started.
Background
- In our organization, one of our project have Power Automate approval process.
- In that process manager have to approve the user’s request.
- But the Power Automate flow failed to get the resource manager.
- We checked in the Azure Active Directory, we found the user don’t have manager.
- We downloaded the users list from Azure AD to check all the user’s manager, but we didn’t get the manager of the users in the file.
- Then we wrote a PowerShell script to get all the managers of the Azure AD users.
- Now we identify the user’s don’t have manager and fixed it.
Details
- Open PowerShell ISE.
- Now ran the PowerShell with the following cmdlet to get Site Collection Data.
Note: The line starting with “#” are just description heading or comment.
Prerequisites
- Install PowerShell ISE / PowerShell / Visual Studio Code if its not installed.
Detailed Steps – PowerShell script
- Connect to the Azure Active Directory
# Connect Azure Directory
Connect-AzureAD
- Use a variable for the outcome.
# Variable for outcome
$outcome = @()
- Get all the Azure AD Users
# Get the Azure AD Users
$users = Get-AzureADUser -All $true
- Apply a loop for each user in the users to get the manager.
# Apply loop to each user
foreach ($user in $users)
- Get the manager of the Azure AD user
# Get the manager of the User
$manager = Get-AzureADUserManager -ObjectId $user.ObjectId
- Use a variable for the properties of Azure AD user and manager.
# Variable for object
$info = New-Object -TypeName psobject
- Get the properties of Azure AD User.
# Get the user properties
$info | Add-Member -MemberType NoteProperty -Name UserDisplayName -Value $user.DisplayName
$info | Add-Member -MemberType NoteProperty -Name UserJobTitle -Value $user.JobTitle
$info | Add-Member -MemberType NoteProperty -Name UserUPN -Value $user.UserPrincipalName
- Get the properties of Azure AD Manager.
# Get the manager properties
$info | Add-Member -MemberType NoteProperty -Name ManagerDisplayName -Value $manager.DisplayName
$info | Add-Member -MemberType NoteProperty -Name ManagerJobTitle -Value $manager.JobTitle
$info | Add-Member -MemberType NoteProperty -Name ManagerUPN -Value $manager.UserPrincipalName
- Collect all the Azure AD user and manager properties
# Assemble the properties in variable
$outcome += $info
- Export the outcome data into CSV file.
# Export the details in CSV file
$outcome | Export-Csv -Path adusers.csv -NoTypeInformation
- Open the exported CSV file.
# Open the CSV file
Invoke-Item adusers.csv
Complete PowerShell Script
<#
===========================================================================================================================
Name: Get the manager of user in Azure Active Directory through PowerShell
Description: This script helps to get the manager of Azure Active Directory users in an Organization through PowerShell
Version: 1.0
===========================================================================================================================
#>
# Connect Azure Directory
Connect-AzureAD
# Variable for outcome
$outcome = @()
# Get the Azure AD Users
$users = Get-AzureADUser -All $true
# Apply loop to each user
foreach ($user in $users) {
# Get the manager of the User
$manager = Get-AzureADUserManager -ObjectId $user.ObjectId
# Variable for object
$info = New-Object -TypeName psobject
# Get the user properties
$info | Add-Member -MemberType NoteProperty -Name UserDisplayName -Value $user.DisplayName
$info | Add-Member -MemberType NoteProperty -Name UserJobTitle -Value $user.JobTitle
$info | Add-Member -MemberType NoteProperty -Name UserUPN -Value $user.UserPrincipalName
# Get the manager properties
$info | Add-Member -MemberType NoteProperty -Name ManagerDisplayName -Value $manager.DisplayName
$info | Add-Member -MemberType NoteProperty -Name ManagerJobTitle -Value $manager.JobTitle
$info | Add-Member -MemberType NoteProperty -Name ManagerUPN -Value $manager.UserPrincipalName
# Assemble the properties in variable
$outcome += $info
}
# Export the details in CSV file
$outcome | Export-Csv -Path adusers.csv -NoTypeInformation
# Open the CSV file
Invoke-Item adusers.csv
Conclusion
Thank you so much for reading this post!
Here’s what the CSV file data looks like:

Hope this article will help us to Get the manager of Azure AD users and export to CSV file through PowerShell.
Also get my article updates on my social media handles.
LinkedIn – https://www.linkedin.com/in/khasim-shaik-8784a1232/
Twitter – https://twitter.com/KhasimShaik2009
Facebook – https://www.facebook.com/profile.php?id=100078255554660
Thank you for your support, will catch up with new article soon.
Keep learning and keep smiling.
Thanks.
You must log in to post a comment.