How to bulk edit user's manager in Entra

Munin O 20 Reputation points
2024-12-12T04:40:43.9933333+00:00

Hi All,

I was trying to bulk edit the user's properties. I have completed the Department, Username, etc. But I cannot edit the Manager Field.

I have got this error

Update-MgUser : Cannot process argument transformation on parameter 'Manager'. Cannot convert the "Name of the Manager" value of type "System.String" to type

Note: I have tried using Object ID, userprincipal name (email), displayname, Staff ID, . But still failed. These are in my CSV file.

Here is my code:

# Connect to Microsoft Graph

Connect-MgGraph -Scopes User.ReadWrite.All

# Read the CSV file

$users = Import-Csv -Path "C:\temp\ManagerUpdate.csv"

# Go through each user in the CSV and update the EmployeeId property

foreach ($user in $users)

{

$userPrincipalName = $user.UserPrincipalName

$employeeId = $user.EmployeeId

$JobTitle = $user.Jobtitle

$Department = $user.Department

$Manager = $user.Manager

# Check if the user exists

$existingUser = Get-MgUser -UserId $userPrincipalName -ErrorAction SilentlyContinue

if ($existingUser)

{

Update-MgUser -UserId $userPrincipalName -JobTitle $jobTitle

Update-MgUser -UserId $userPrincipalName -Department $Department

Update-MgUser -UserId $userPrincipalName -EmployeeId $employeeId

Update-MgUser -UserId $userPrincipalName -Manager $Manager

}

else

{

echo "end of program with error"

}

echo "Done for '$userPrincipalName' "

}

Disconnect-MgGraph

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,113 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,822 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,340 questions
0 comments No comments
{count} votes

Accepted answer
  1. Beezy 75 Reputation points
    2025-01-31T16:48:05.8133333+00:00

    The following worked for me.
    Note: I got the .csv from the Entra admin panel. Entra gave me the .csv with the column named "Id" instead of "userID". All examples I found had the script referencing "userID".

    To use this script, the column on your CSV must be named "Id"

    # Connect to Microsoft Graph
    Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"
    # Import the CSV file
    $csv = Import-Csv -Path 'REPLACE WITH PATH TO YOUR CSV'
    # Retrieve all users
    $allUsers = Get-MgUser -All
    # Iterate through each row in the CSV file
    foreach ($row in $csv) {
        if (![string]::IsNullOrEmpty($row.id)) {
            # Retrieve the current manager of the user
            $currentManager = Get-MgUserManager -UserId $row.id -ErrorAction SilentlyContinue
            if ($currentManager) {
                Write-Host "User $($row.id) already has a manager assigned. Skipping..."
            } else {
                # Retrieve the manager's ID based on the display name
                $manager = $allUsers | Where-Object { $_.displayName -eq $row.managerDisplayName }
                if ($manager) {
                    $NewManager = @{
                        '@odata.id' = "https://graph.microsoft.com/v1.0/users/$($manager.Id)"
                    }
                    # Update the user's manager
                    Set-MgUserManagerByRef -UserId $row.id -BodyParameter $NewManager
                } else {
                    Write-Host "Manager with display name $($row.managerDisplayName) not found."
                }
            }
        } else {
            Write-Host "UserId is empty for one of the rows. Skipping..."
        }
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Vasil Michev 114.1K Reputation points MVP
    2024-12-12T08:12:47.3933333+00:00

    "Manager" is a reference property, you cannot update it like that. Use the Set-MgUserManagerByRef cmdlet instead, see the example in the documentation for more details: https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users/set-mgusermanagerbyref?view=graph-powershell-1.0#example-1-update-a-users-manager


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.