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
}
$User
}
$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.