Forum Discussion
Bulk update Azure AD with user attributes from CSV
- May 08, 2020
Hello Jacob,
Your CSV has to look something like this:
UserPrincipalName;Department;TelephoneNumber
manfreddelaat@domain.nl;IT;0135113333
manfred@domain.nl;IT;0622222222Your Powershell code:
# Connect to AzureAD Connect-AzureAD # Get CSV content $CSVrecords = Import-Csv C:\Temp\Test.csv -Delimiter ";" # Create arrays for skipped and failed users $SkippedUsers = @() $FailedUsers = @() # Loop trough CSV records foreach ($CSVrecord in $CSVrecords) { $upn = $CSVrecord.UserPrincipalName $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'" if ($user) { try{ $user | Set-AzureADUser -Department $CSVrecord.Department -TelephoneNumber $CSVrecord.TelephoneNumber } catch { $FailedUsers += $upn Write-Warning "$upn user found, but FAILED to update." } } else { Write-Warning "$upn not found, skipped" $SkippedUsers += $upn } } # Array skipped users # $SkippedUsers # Array failed users # $FailedUsers
Good luck!
Kind Regards, Manfred de Laat
I already have the AzureAD module installed. I've already updated all the other fields with no issues.
Going back to the documentation for Set-AzureADUser (https://docs.microsoft.com/en-us/powershell/module/azuread/set-azureaduser?view=azureadps-2.0) I do not see "Manager" listed as a parameter.
The appropriate command is set-azureadusermanager
you will need the azureAD objectid of the manager, which we will have to first call. So this script will not work as is.
$user=$CSVrecord.UserPrinicipalName
$Manager= $CSVrecord.Manager
$ManagerObj=Get-AzureADUser -Objectid $Manager
Set-AzureADUserManager -ObjectId $User -RefObjectId $ManagerObj.ObjectId
In your specific instance, it may be simple to populate the csv with the objectid of the single manager or the script with the objectid as only one manager exists.
- jebujohnAug 18, 2021Copper ContributorTry a csv with UserPrincipalName, Manager where both are UPNs. The UserPrincipalName for the user whom you want to assign a manager and under the manager the UPN of the manager
- vipfafenAug 18, 2021Copper ContributorThanks for the quick response, but I run into mistakes. My CSV is structured as follows:
UserPrincipalName,"ManagerName"
<Username.username>#EXT#@<domain>,"<Managername>, <Managersurname>"
<username.username>@<domain2>,"<Managername>, <Managersurname>"
... - jebujohnAug 18, 2021Copper Contributor
$CSVrecords = Import-Csv test.csv -Delimiter "," foreach ($CSVrecord in $CSVrecords) { $usr = $CSVrecord.UserPrincipalName $manager = $CSVrecord.Manager $user = Get-AzureADUser -Filter "userPrincipalName eq '$usr'" $Managerobj = Get-AzureADUser -Filter "userprincipalname eq '$manager'" if ($user) { try{ $user | Set-AzureADUserManager -RefObjectid $Managerobj.objectid } catch { $FailedUsers += $usr Write-Warning "$usr user found, but FAILED to update." } } else { Write-Warning "$usr not found, skipped" $SkippedUsers += $usr } }
- vipfafenAug 18, 2021Copper Contributor@arnelianfox did you get a working script? Can't get it to work either
- carnelianfoxMar 02, 2021Copper Contributor
Jacob John could you please write out the script in full. It's a bit confusing the way you put it. I am unable to correctly update User Manager. Thanks,