Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitIn many organizations, it is important to keep track of reporting structures—specifically which users report to which managers. This information can be useful for HR audits, IT governance, or access control reviews. With Microsoft Graph PowerShell, you can quickly pull this data and even email it as a report to the administrator.
The following script fetches all users along with their manager’s Display Name, UPN, and Email, exports the details to a CSV file, and sends the report via email.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All","Mail.Send","Directory.Read.All"
# ---------------------------------------------
# 1) Fetch all users with manager expanded
# ---------------------------------------------
$Users = Get-MgUser -All -Property DisplayName,UserPrincipalName,Mail,Manager -ExpandProperty Manager
# ---------------------------------------------
# 2) Shape the data for export
# ---------------------------------------------
$ReportRows = foreach ($user in $Users) {
$ManagerDisplayName = $null
$ManagerUPN = $null
$ManagerMail = $null
if ($user.Manager) {
$ManagerDisplayName = $user.Manager.AdditionalProperties.displayName
$ManagerUPN = $user.Manager.AdditionalProperties.userPrincipalName
$ManagerMail = $user.Manager.AdditionalProperties.mail
}
[pscustomobject]@{
UserDisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
UserMail = $user.Mail
ManagerDisplayName = $ManagerDisplayName
ManagerUPN = $ManagerUPN
ManagerMail = $ManagerMail
}
}
# ---------------------------------------------
# 3) Export to CSV
# ---------------------------------------------
$ReportPath = "$env:TEMP\UsersWithManagers.csv"
$ReportRows | Export-Csv -Path $ReportPath -NoTypeInformation -Encoding UTF8
# ---------------------------------------------
# 4) Email the report to the administrator
# ---------------------------------------------
$AdminUPN = "admin@yourtenant.onmicrosoft.com" # <-- Replace with your admin mailbox
$Subject = "Users with Managers Report - $(Get-Date -Format 'yyyy-MM-dd')"
$Body = @"
Hello Admin,
Please find attached the latest list of users along with their manager details.
Regards,
Graph PowerShell Script
"@
# Read and attach the CSV as a fileAttachment
$AttachmentContent = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($ReportPath))
$Attachments = @(
@{
"@odata.type" = "#microsoft.graph.fileAttachment"
Name = "UsersWithManagers.csv"
ContentBytes = $AttachmentContent
}
)
# Build the message payload (BodyParameter)
$Message = @{
Message = @{
Subject = $Subject
Body = @{
ContentType = "HTML"
Content = $Body
}
ToRecipients = @(
@{
EmailAddress = @{ Address = $AdminUPN }
}
)
Attachments = $Attachments
}
SaveToSentItems = "true"
}
# Send the email
Send-MgUserMail -UserId $AdminUPN -BodyParameter $Message
Write-Host "Users with managers (extended) report emailed successfully to $AdminUPN"
This ensures the administrator has a clear picture of the reporting hierarchy in the tenant.
Error | Cause | Solution |
---|---|---|
Insufficient privileges to complete the operation | Missing delegated Graph API permissions. | Ensure you use User.Read.All, Directory.Read.All, and Mail.Send scopes. |
Send-MgUserMail : Resource not found | $AdminUPN is invalid or not a mailbox-enabled user. | Update $AdminUPN to a valid mailbox address in your tenant. |
Empty Manager Columns | Some users do not have managers assigned. | This is expected behavior. Empty values indicate no manager is defined. |
CSV Attachment Missing | The file was not created or permission was denied. | Verify $ReportPath and ensure PowerShell has rights to write to $env:TEMP. |
This script provides administrators with a straightforward way to fetch users and their managers and deliver the report directly to their inbox. By automating the process, it saves time, improves visibility into reporting relationships, and supports HR and IT governance needs.
With a few enhancements like scheduling and filtering, this script can be turned into a valuable recurring report for organizational management.
© m365corner.com. All Rights Reserved. Design by HTML Codex