How to bulk update Manager for many users on AzureAD using .csv import

Long Tran 0 Reputation points


I have a .csv with 2 columns: UserPrincipalName and Manager. I would like to import this .csv and bulk update the Manager attribute for users based on their UserPrincipalName.

Please help with the PowerShell script for this.

Thank you.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,544 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    Comments have been turned off. Learn more

  2. 2023-12-06T23:10:22.1666667+00:00

    Hello @Long Tran, in order to update managers for a set of Entra ID users from data stored in a CSV you can use the BulkUpdate-UserManager.ps1 PowerShell script. Below you will find a simplified version:

    $ErrorActionPreference = 'Stop'
    $TenantId = "string"
    $CsvFilePath = "string"
    $ResultCsvPath = "string"
    $ClientId = "string"
    $token = Get-MsalToken -ClientId $ClientId -TenantId $TenantId -Prompt SelectAccount -ForceRefresh
    Connect-MgGraph -AccessToken $token.AccessToken -Scopes "User.ReadWrite.All"
    $CsvUsers = Import-Csv -Path $CsvFilePath -Encoding UTF8
    $Results = foreach ($User in $CsvUsers) {
      try {
        $UserPrincipalName = $User.UserPrincipalName
        $ManagerValue = $User.ManagerUserPrincipalName
        if ($UserPrincipalName -notmatch "\S") {
          throw "UserPrincipalName is empty"
        if ($ManagerValue -notmatch "\S") {
          throw "ManagerUserPrincipalName is empty"
        $EntraIdUser = Get-MgUser -UserId $UserPrincipalName
        $Manager = Get-MgUser -Filter "UserPrincipalName eq '$ManagerValue'"
        $ManagerRef = New-Object Microsoft.Graph.DirectoryObjectIdentityReference -ArgumentList $Manager.Id
        Set-MgUser -Identity $EntraIdUser.Id -Manager $ManagerRef
        $User | Add-Member -NotePropertyName "Status" -NotePropertyValue "Success"
      catch {
        $User | Add-Member -NotePropertyName "Status" -NotePropertyValue "Error"
        $User | Add-Member -NotePropertyName "Details" -NotePropertyValue $_.Exception.Message
    $Results | Export-Csv -Path $ResultCsvPath -NoTypeInformation

    Let us know if you need additional assistance. If the answer was helpful, please accept it and rate it so that others facing a similar issue can easily find a solution.