đź”§ New: User Management Graph PowerShell Toolkit

Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.

🚀 Launch Toolkit

Fetch & Email Users With Managers Using Graph PowerShell

In 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.


i) Script



# 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"

ii) How the Script Works

  1. Authentication – The script connects to Microsoft Graph with scopes User.Read.All, Directory.Read.All, and Mail.Send.
  2. Fetch Users with Managers – Using -ExpandProperty Manager, the script retrieves each user’s details and their manager’s properties in one query.
  3. Builds a Report – For each user, it extracts:
    • User’s Display Name, UPN, and Email
    • Manager’s Display Name, UPN, and Email
  4. Export to CSV – The results are stored in a CSV file in the temp folder.
  5. Email the Report – The CSV is attached to an email and sent to the administrator’s mailbox using Send-MgUserMail.

This ensures the administrator has a clear picture of the reporting hierarchy in the tenant.


iii) Further Enhancements

  • Add Manager’s Manager – Extend the script to include hierarchical reporting (multi-level managers).
  • Schedule Automation – Use Task Scheduler or Azure Automation to run the report on a weekly/monthly basis.
  • Filter Users – Fetch users only from specific departments or locations.
  • Export to SharePoint/OneDrive – Store the report centrally for collaboration instead of emailing it.
  • Include Additional Attributes – Add properties like job title, department, or office location for richer context.

iv) Possible Errors & Solutions

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.

v) Conclusion

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.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved. Design by HTML Codex