Search code examples
powershellazure-active-directorybulk

AzureAD Powershell Script to Bulk Change Manager Field


  1. My goal would be to have a Powershell script that can import a CSV to bulk change a users manager field in AzureAD. The CSV would have 2 columns, one with the user and the other with their manager.
  2. I've found scripts to export all users from AzureAD into a CSV, but this doesn't contain a column header for the manager field. I found an AzureAD script than can change the manager field using objectID but that's cumbersome, so ideally I could use an email address for the manager field.
  3. I don't have code to show really, these were pretty basic scripts I found but I'm at best a non Powershell user.

Solution

  • Let us assume that you have below file :

    enter image description here

    In the left you have the username of the user and on the right you have the username of the new manager.

    You could use the below snippet

    #connecting to the Azure AD
    Connect-AzureAD 
    
    #importing the CSV source which has the changes 
    $data = Import-Csv D:\Temp\Book1.csv
    
    #Iterating through each row in the CSV
    foreach ($row in $data)
    {
    #INFO in the Console
    Write-Host "Updating the user :"  $row.'User Username'    " manager to "  $row.'Manager Username'  -ForegroundColor Yellow 
    
    #Updating the Manager 
    Set-AzureADUserManager -ObjectId (Get-AzureADUser -ObjectId $row.'User Username').Objectid -RefObjectId (Get-AzureADUser -ObjectId $row.'Manager Username').Objectid
    
    #Completion info in the console for the specified row
    Write-Host "Updated." -ForegroundColor Green
    
    }
    

    Explanation :

    Step 1 : Connecting to the Azure AD

    Step 2: Importing the CSV data that needs to be bulk updated

    Step 3 : Iterating through each row, updating the manager field using the commandlet Set-AzureADUserManager

    Sample output :

    enter image description here