The Set-MgUserManagerByRef cmdlet in Microsoft Graph PowerShell is used to assign or update a manager for a user in Microsoft 365. Instead of directly modifying user attributes, it creates a reference between a user and their manager, aligning with organizational hierarchy management.
This cmdlet is particularly useful when:
By using PowerShell, administrators save time and reduce errors compared to manual updates in the Admin Center.
Before running this cmdlet, connect to Microsoft Graph with the required permissions:
Connect-MgGraph -Scopes "User.ReadWrite.All"
Syntax (essential parameters):
Set-MgUserManagerByRef -UserId <String> -BodyParameter <Hashtable>
You must provide the UserId (UPN or ObjectId) of the user and specify the manager reference using @odata.id.
$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 a manager with the given object ID to John Doe.
$Manager = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/87654321-abcd-efgh-ijkl-mnopqrstuvwx"
}
$Users = @("user1@company.com", "user2@company.com", "user3@company.com")
foreach ($User in $Users) {
Set-MgUserManagerByRef -UserId $User -BodyParameter $Manager
}
This loop assigns the same manager to multiple users at once.
CSV File Format:
UserPrincipalName,ManagerId
user1@company.com,manager1-id
user2@company.com,manager2-id
PowerShell Script:
$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
}
This assigns managers to multiple users by reading from a CSV file, useful for bulk updates.
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.
© Your Site Name. All Rights Reserved. Design by HTML Codex