Managing user hierarchies and relationships in Microsoft 365 can be simplified using Graph PowerShell. The Get-MgUserDirectReport cmdlet is a powerful tool for fetching the direct reports of a specific user. This article explores the cmdlet's syntax, usage examples, tips, common errors, and impressive use cases.
Get-MgUserDirectReport -UserId <String>
-UserId: User whose direct reports you want to retrieve. It accepts either a User Principal Name (e.g., samadmin@contoso.com) or Object ID.
This example demonstrates how to use the cmdlet interactively by first typing the command and then entering the UserId when prompted.
Get-MgUserDirectReport
This example showcases how to provide the UserId directly within the command.
Get-MgUserDirectReport -UserId "samadmin@7xh7fj.onmicrosoft.com"
Both examples return the list of users (only their User IDs) reporting directly to the specified manager. If you want details like UserPrincipalName and DisplayName, then you need to pass these IDs to the Get-MgUser cmdlet as shown in the image below.
Select-Object
to narrow down the output.Get-MgUserDirectReport -UserId "samadmin@7xh7fj.onmicrosoft.com" | Select-Object Id, DisplayName, JobTitle
Get-MgUserDirectReport -UserId "samadmin@7xh7fj.onmicrosoft.com" | Export-Csv -Path "DirectReports.csv" -NoTypeInformation
Error | Cause | Solution |
---|---|---|
ResourceNotFound | The UserId provided does not exist or is incorrect. | Verify the UserId with Get-MgUser . |
Authorization_RequestDenied | Insufficient permissions for the signed-in account. | Assign the User.Read.All or Directory.Read.All role. |
InvalidAuthenticationToken | Authentication token is expired or invalid. | Reauthenticate using Connect-MgGraph . |
CommandNotFoundException | Cmdlet not recognized due to missing module. | Ensure the Microsoft Graph module is installed. |
Empty Output (No Direct Reports Returned) | The user has no direct reports. | Confirm user hierarchy or validate data in Azure AD. |
# Loop through a list of managers and export direct reports
$Managers = @("manager1@contoso.com", "manager2@contoso.com")
foreach ($Manager in $Managers) {
Get-MgUserDirectReport -UserId $Manager | Export-Csv -Path "$($Manager)_DirectReports.csv" -NoTypeInformation
}
The Get-MgUserDirectReport cmdlet is an essential tool for managing user relationships in Microsoft 365. By providing straightforward access to user hierarchies, it enables administrators to streamline operations, enhance reporting, and support organizational transparency. Whether you're auditing user structures or automating managerial tasks, this cmdlet is a valuable addition to your administrative toolkit.
© m365corner.com. All Rights Reserved. Design by HTML Codex