Update Manager for Bulk Azure AD Users using PowerShell

In this blog, we will explore how to set or update the Manager field in Azure AD for Microsoft Office 365 users in bulk using PowerShell.

We can use the Set-AzureADUser cmdlet to update normal user properties in Azure AD. But we need to use the Set-AzureADUserManager cmdlet to set the manager and use the Remove-AzureADUserManager cmdlet to remove or clear the existing manager attribute value.

Before you start, install the latest Azure AD PowerShell module and run the following command to connect the module.

Connect-AzureAD

Run the following commands to update the manager property for a single user in Azure Active Directory.

$UserUPN  = "[email protected]"
$ManagerUPN  = "[email protected]"

#Get the manager's ObjectId using the UPN
$ManagerObj = Get-AzureADUser -ObjectId $ManagerUPN

#Set the manager for the specified user in Azure AD
Set-AzureADUserManager -ObjectId $UserUPN -RefObjectId $ManagerObj.ObjectId

You can use the Get-AzureADUserManager cmdlet to retrieve a user’s existing manager value.

Get-AzureADUserManager -ObjectId "[email protected]"

Update Manager for Bulk Microsoft 365 users from CSV

To update the manager for users in bulk, we can keep the required user details in a CSV file. Consider the CSV file “AzureADUsersManager.csv” (Download sample CSV) which holds the required user’s UPN and their manager UPN (UserPrincipalName) in each row with the column headers UserUPN and ManagerUPN.

The following script imports the user details from the CSV file and sets the manager attribute in Azure AD for users one by one.

#Read user details from the CSV file
$CSVRecords = Import-CSV "C:\Temp\AzureADUsersManager.csv"
$i = 0;
$TotalRows = $CSVRecords.Count

#Array to add the status result
$UpdateResult=@()

#Iterate CSVRecords (users) and set manager for users one by one
Foreach($CSVRecord in $CSVRecords)
{
$UserUPN = $CSVRecord.'UserUPN'
$ManagerUPN = $CSVRecord.'ManagerUPN'

$i++;
Write-Progress -activity "Processing $UserUPN (Manager-$ManagerUPN)" -status "$i out of $TotalRows users completed"

Try
{

#Set-AzureADUserManager cmdlet - the RefObjectId parameter requires the manager's ObjectId.
#The below command retrieves the ObjectId using the manager's UPN
$ManagerObj = Get-AzureADUser -ObjectId $ManagerUPN

#Set the manager
Set-AzureADUserManager -ObjectId $UserUPN -RefObjectId $ManagerObj.ObjectId
#Set update status
$UpdateStatus = "Success"
}
catch
{
$UpdateStatus = "Failed: $_"
}

#Add update status to the result array
$UpdateResult += New-Object PSObject -property $([ordered]@{
UserUPN = $UserUPN
ManagerUPN = $ManagerUPN
Status = $UpdateStatus
})

}

#Display the update status result 
$UpdateResult | Select UserUPN,ManagerUPN,Status

#Export the update status report to a CSV file
#$UpdateResult | Export-CSV "C:\Temp\UpdateManagerStatus.CSV" -NoTypeInformation -Encoding UTF8

Note: Here, we are setting up the user’s manager field in Azure AD, the update does not immediately reflect in other places such as the user’s Exchange Mailbox and SharePoint Online User Profile. You need to wait a few mins to hours for a full crawl of Active Directory by the SharePoint User Profiles.

Advertisement

1 thought on “Update Manager for Bulk Azure AD Users using PowerShell”

Leave a Comment