In Microsoft 365, understanding reporting hierarchies is essential for managing teams and user relationships. The Get-MgUserDirectReport cmdlet retrieves the direct reports of a user, but by default, it only provides their User IDs. To fetch additional details, such as Display Name, User Principal Name (UPN), and Email, you need to pair it with the Get-MgUser cmdlet. This article highlights the necessity of nesting these cmdlets and demonstrates how to fetch detailed information about direct reports.
The following script retrieves the direct reports of a specific user and uses Get-MgUser to fetch their personal details:
# Retrieve the detailed information of direct reports
$managerId = "john.manager@yourdomain.com" # Replace with the manager's UserPrincipalName
$directReports = Get-MgUserDirectReport -UserId $managerId
foreach ($report in $directReports) {
Get-MgUser -UserId $report.Id | Select-Object DisplayName, Mail, UserPrincipalName
}
Using Get-MgUserDirectReport gets only User Ids
Using Get-MgUserDirectReport with Get-MgUser gets detailed User Info like Display Name, UserPrincipalName
Get-MgUserDirectReport cmdlet fetches the direct reports of a specific user using the manager's UserPrincipalName or Object ID.Get-MgUser cmdlet retrieves additional details for each direct report by using their User ID.foreach loop ensures details are retrieved for each direct report individually.Get-MgUserDirectReport with Get-MgUser is essential to retrieve additional properties like Display Name and Email.Select-Object cmdlet to display only the required fields, making the output concise and clear.$results = foreach ($report in $directReports) {
Get-MgUser -UserId $report.Id | Select-Object DisplayName, Mail, UserPrincipalName
}
$results | Export-Csv -Path "DirectReportsDetails.csv" -NoTypeInformation
| Error | Cause | Solution |
| The specified object was not found in the directory. | The manager's ID is incorrect or does not exist in the directory. | Verify the manager's ID using Get-MgUser:
|
| Insufficient privileges to complete the operation. | Missing permissions like User.Read.All or Directory.Read.All. |
Ensure the account running the script has these permissions. |
| No direct reports found. | The user has no direct reports assigned in the directory. | Confirm the user's reporting relationships in Azure AD. |
| Request throttled due to too many API calls. | Too many requests being sent to retrieve user details. | Implement delays between requests or process data in batches. |
Pairing Get-MgUserDirectReport with Get-MgUser is essential for retrieving detailed information about direct reports in Microsoft 365. By default, Get-MgUserDirectReport only provides User IDs, but with the addition of Get-MgUser, administrators can extract personal details such as Display Name, UPN, and Email. This combination simplifies team management, supports compliance reporting, and ensures a clear understanding of organizational structures. Start using this approach today to streamline your workflows!
© m365corner.com. All Rights Reserved. Design by HTML Codex