tdannecy@gmail.com

Simplify Azure AD manager updates with PowerShell and a CSV file

#PowerShell #AzureAD

After a domain migration or other large organizational change, you may need to update hundreds of staff at the same time with manager information in Azure AD.

This can be a huge pain to do it through the GUI and there's an easier way to do it with PowerShell and a CSV file.

To perform this task, you'll need an account with the Azure AD Role of at least User Administrator. You will also need a Windows computer running PowerShell 5.1

First, get all current staff and managers from Azure AD and export them to a .csv file:

Install-Module AzureAD
Connect-AzureAD
$users = Get-AzureADUser -All $true | Where-Object { $_.UserType -ne 'Guest' } | foreach {
    $Manager = Get-AzureADUserManager -ObjectID $_.ObjectID 
    New-Object psobject -Property @{
        StaffDisplayName         = $_.DisplayName
        StaffUserPrincipalName   = $_.UserPrincipalName
        ManagerDisplayName       = $Manager.DisplayName
        ManagerUserPrincipalName = $Manager.UserPrincipalName
    }
}

$users | Select-Object StaffDisplayName, StaffUserPrincipalName, ManagerDisplayName, ManagerUserPrincipalName | Export-Csv -Path 'c:\users.csv' -notypeinformation 

This will output a .csv file at c:\users.csv and will have the following columns and formatting:

StaffDisplayName StaffUserPrincipalName ManagerDisplayName ManagerUserPrincipalName
John Smith john.smith@example.com Jane Doe jane.doe@example.com

Then, open the .csv file and update the manager fields for each staff you want to update. You can enter the manager's email address/UPN in column ManagerUserPrincipalName or you can use the manager's display name (firstname lastname) in column ManagerDisplayName. I haven't tried the script with both, so save yourself a headache just use one or the other like this:

StaffDisplayName StaffUserPrincipalName ManagerDisplayName ManagerUserPrincipalName
John Smith john.smith@example.com Guy Person

After that, save the file and run this PowerShell script:

Import-csv -Path 'C:\users.csv' | ForEach-Object {
    if ($_.ManagerUserPrincipalName) {
        Write-Host "Updating manager for user $($_.StaffDisplayName) as $($_.ManagerUserPrincipalName)"
        if ($_.StaffUserPrincipalName) {
            Set-AzureADUserManager -ObjectId $_.StaffUserPrincipalName -RefObjectId (Get-AzureADUser -ObjectID $_.ManagerUserPrincipalName).ObjectID
        }
        else {
            Write-Host "No StaffUserPrincipalName found for user $($_.StaffDisplayName)"
        }
    }
    elseif ($_.ManagerDisplayName) {
        Write-Host "Updating manager for user $($_.StaffDisplayName) as $($_.ManagerDisplayName)"
        if ($_.StaffUserPrincipalName) {
            Set-AzureADUserManager -ObjectId $_.StaffUserPrincipalName -RefObjectId $(Get-AzureADUser -Filter "displayName eq '$($_.ManagerDisplayName)'").ObjectID
        }
        else {
            Write-Host "No StaffUserPrincipalName found for user $($_.StaffDisplayName)"
        }
    }
    else {
        Write-Host "No manager found for user $($_.StaffDisplayName)"
    }
}

I hope this helps!

Discuss...