The Set-MgUserManagerByRef cmdlet, part of the Microsoft Graph PowerShell module, allows administrators to programmatically assign or update a user’s manager. This guide provides a comprehensive walkthrough of its usage, including practical examples and best practices.
Who Are Microsoft 365 User Managers?
A Microsoft 365 user manager represents the person to whom a user directly reports. The manager relationship is essential for:
- Approval Workflows: Managers can approve leave, expenses, and other requests.
- HR Insights: Providing managers with access to relevant employee data.
- Compliance: Maintaining accurate records of organizational hierarchy.
Why Use Set-MgUserManagerByRef?
The Set-MgUserManagerByRef cmdlet simplifies assigning or updating managers for one or multiple users. Benefits include:
- Automation: Update manager relationships in bulk, saving time and effort.
- Consistency: Ensure accuracy across large organizations with multiple users.
- Integration: Incorporate into workflows for onboarding, role changes, or department shifts.
Setting Up Microsoft Graph PowerShell
To use Set-MgUserManagerByRef, you need to set up Microsoft Graph PowerShell:
- Install the Module:
Install-Module Microsoft.Graph -Scope CurrentUser - Connect to Microsoft Graph:
Connect-MgGraph -Scopes "User.Read.All" - Disconnect After Use:
Disconnect-MgGraph
Exploring the Set-MgUserManagerByRef Cmdlet
The Set-MgUserManagerByRef cmdlet assigns or updates the manager for a specified user. It uses an @odata.id reference pointing to the manager’s unique Object ID.
Cmdlet Syntax
Set-MgUserManagerByRef -UserId <String> -BodyParameter <Object> [<CommonParameters>l]
Usage Examples
1. Assign a Manager to a Single User
To assign a specific manager to a user:
$Manager = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/12345678-90ab-cdef-ghij-klmnopqrstuv"
}
Set-MgUserManagerByRef -UserId "john.doe@company.com" -BodyParameter $Manager
This assigns the manager with the Object ID 12345678-90ab-cdef-ghij-klmnopqrstuv to the user john.doe@company.com.
2. Assign Managers in Bulk Using a CSV File
For large-scale updates, use a CSV file to define users and their corresponding managers.
CSV File Format
UserPrincipalName,ManagerId
user1@company.com,manager1-id
user2@company.com,manager2-id
$CSVPath = "C:\Managers.csv"
$Users = Import-Csv -Path $CSVPath
foreach ($User in $Users) {
$Manager = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$($User.ManagerId)"
}
Set-MgUserManagerByRef -UserId $User.UserPrincipalName -BodyParameter $Manager
}
Best Practices for Using Set-MgUserManagerByRef
- Validate Manager and User Accounts: Before assigning a manager, ensure both the user and manager accounts are valid and active. Use Get-MgUser to verify their details.
- Log Changes: Keep a log of successful updates and errors for auditing purposes. Example:
try {
Set-MgUserManagerByRef -UserId $userId -BodyParameter $Manager
Add-Content -Path "C:\Logs\ManagerUpdates.txt" -Value "Assigned manager to $userId successfully."
} catch {
Add-Content -Path "C:\Logs\ManagerUpdates.txt" -Value "Failed to assign manager to $userId: $_"
}
Set-MgUserManagerByRef -UserId "john.doe@company.com" -BodyParameter $Manager -WhatIf
Conclusion
The Set-MgUserManagerByRef cmdlet is an indispensable tool for Microsoft 365 administrators. Whether assigning a manager to a single user or updating reporting structures for entire departments, this cmdlet simplifies the process and ensures organizational hierarchies remain accurate.
By leveraging the power of Set-MgUserManagerByRef and following best practices, you can save time, reduce errors, and improve the efficiency of your administrative workflows.
Start using Set-MgUserManagerByRef today and streamline your organization’s hierarchy management!
🚀 Community Edition Released!
Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.
Get it on GitHub Know More