Table of Contents
Method 1: Using Microsoft Graph Powershell SDK
Pre-requisites
Before you begin, you need to ensure the following:
- A computer with Windows PowerShell 5.1+ or PowerShell 7+ (Mac or Linux)
- Installed Microsoft Graph PowerShell SDK.
Or you can open a new PowerShell (Terminal) window then run the below commands to install the Microsoft Graph PowerShell SDK.
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force
Once done, connect to Microsoft Graph PowerShell with the following required scopes then sign in using an administrative account or Global admin account.
Connect-MgGraph -Scopes User.Read.All
PowerShell script
Below is a simple script that help you export the list of users with managers in your Microsoft 365 tenant. You can copy then run the code directly in the PowerShell console or you can create a PowerShell script for later use.
# Define the properties to retrieve from the user.
$properties = @('DisplayName','UserPrincipalName','Manager')
# Get a report users from the Microsoft Graph.
$users = Get-MgUser -All -Property $properties -ExpandProperty 'Manager'
# Build a custom report with details.
$report = $users | ForEach-Object {
[pscustomobject]@{
DisplayName = $_.DisplayName
UserPrincipalName = $_.UserPrincipalName
ManagerDisplayName = $_.Manager.AdditionalProperties.displayName
ManagerEmail = $_.Manager.AdditionalProperties.mail
}
}
# Output options to console, graphical grid view or export to CSV file.
$report | Format-Table
# $report | Out-GridView
# $report | Export-Csv D:\report.csv
In the script, we’ve added some output options such as show it in the graphical grid view or export it to a CSV file. You can uncomment to enable them.
# Example output
DisplayName UserPrincipalName ManagerDisplayName ManagerEmail
----------- ----------------- ------------------ ------------
Adele Vance [email protected] Miriam Graham [email protected]
MOD Administrator [email protected]
Alex Wilber [email protected] Miriam Graham [email protected]
Allan Deyoung [email protected] Nestor Wilke [email protected]
...
Method 2: Using Microsoft Graph REST API
Alternatively, we can use the Microsoft Graph Rest Api to export a list of users with manager property. When using this method:
- We don’t need to install any modules of the Microsoft Graph PowerShell SDK (~ 80 modules).
- We can do it from any machine with Microsoft PowerShell/ PowerShell Core installed (Linux and macOS). Because it uses the native PowerShell cmdlet Invoke-RestMethod to make a request.
- Instead of using an account for authentication and authorization, we use the app-only access (access without a user).
Once the app has been created, replace your app’s information (clientId, tenantId and the clientSecret) into the below code. Steps in this code:
- Get access token ($token) with app registration client secret.
- Create the request header ($headers) for API call.
- Create requests to the Microsoft Graph resource ($uri) with pagination to get all data.
- Build the report ($result) from API call response.
- Output options to console, graphical grid view or export to CSV file.
# Get access token with app registration client secret.
$clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $clientId
Client_Secret = $clientSecret
}
$tokenRequest = Invoke-RestMethod -Uri $uri -Method POST -Body $body
$token = $tokenRequest.access_token
# Create the request header for API call.
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
# This resource returns users list with accountEnabled detail for requests.
$uri = "https://graph.microsoft.com/beta/users?`$select=displayName,userPrincipalName&`$expand=manager(`$select=displayName,userPrincipalName)&`$top=999"
#Perform pagination if next page link (odata.nextlink) returned.
$result = @()
while ($null -ne $uri) {
$response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers
$users = $response.value
ForEach($user in $users) {
$Result += New-Object PSObject -property $([ordered]@{
UserName = $user.displayName
UserPrincipalName = $user.userPrincipalName
ManagerDisplayName = $user.manager.displayName
ManagerUPN = $user.manager.userPrincipalName
})
}
$uri = $response.'@odata.nextlink'
}
# Output options to console, graphical grid view or export to CSV file.
$result | Format-Table
# $result | Out-GridView
# $result | Export-CSV "C:\Result.csv" -NoTypeInformation -Encoding UTF8
Manage Microsoft 365 Using Microsoft Graph
You can also take a look at the following Microsoft Graph posts that help to manage Microsoft 365 efficiently.
- Create a new user
- Create bulk users in Microsoft 365
- Get a list of all users in Microsoft 365
- Update user properties
- Add a user to a group
- Add bulk users to a group
- Remove users from a group
- Remove multiple users from a group
- Assign managers for Microsoft 365 users
- Assign licenses to users
- Removing licenses from user accounts
- Delete a user from Microsoft 365
- How to use Get-MgUser cmdlet
Not a reader? Watch this related video tutorial: