In Microsoft 365, establishing accurate reporting structures is critical for ensuring smooth workflows, enabling effective approvals, and maintaining organizational integrity.
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.
A Microsoft 365 user manager represents the person to whom a user directly reports. The manager relationship is essential for:
The Set-MgUserManagerByRef cmdlet simplifies assigning or updating managers for one or multiple users. Benefits include:
To use Set-MgUserManagerByRef, you need to set up Microsoft Graph PowerShell:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.Read.All"
Disconnect-MgGraph
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.
Set-MgUserManagerByRef -UserId <String> -BodyParameter <Object> [<CommonParameters>l]
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.
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
}
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
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!
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.