Using Get-MgUserDirectReport with Get-MgUser: Fetch Personal Details of Direct Reports

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.

Usage Example

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

Explanation

  • Retrieve Direct Reports: The Get-MgUserDirectReport cmdlet fetches the direct reports of a specific user using the manager's UserPrincipalName or Object ID.
  • Fetch Personal Details: The Get-MgUser cmdlet retrieves additional details for each direct report by using their User ID.
  • Loop Through Reports: A foreach loop ensures details are retrieved for each direct report individually.

Tips and Best Practices

  • Nesting Cmdlets is Necessary: Since Get-MgUserDirectReport only provides the User ID of the direct reports, pairing Get-MgUserDirectReport with Get-MgUser is essential to retrieve additional properties like Display Name and Email.
  • Optimize the Output: Use the Select-Object cmdlet to display only the required fields, making the output concise and clear.
  • Export Results for Documentation: Export the retrieved data to a CSV file for reporting or analysis:
    $results = foreach ($report in $directReports) {
        Get-MgUser -UserId $report.Id | Select-Object DisplayName, Mail, UserPrincipalName
    }
    $results | Export-Csv -Path "DirectReportsDetails.csv" -NoTypeInformation
  • Handle Large Data Sets: Ensure your script can handle large data sets efficiently by using batch processing or limiting properties.

Possible Errors & Solutions

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:

Get-MgUser -Filter "userPrincipalName eq 'john.manager@yourdomain.com'"
                                            
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.

Use Cases

  • Generate Reports on Reporting Structures: Create reports on team hierarchies, listing managers and their direct reports with additional details such as Display Name, UPN, and Email.
  • Troubleshoot Organizational Changes: Verify the accuracy of reporting relationships during organizational restructuring or user transfers.
  • Audit Compliance: Identify all team members under a specific manager to ensure compliance with internal policies or regulatory requirements.
  • Enhance Access Management: Retrieve details of direct reports to manage access permissions and privileges for group resources or applications.

Conclusion

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