Forum Discussion
jebujohn
May 08, 2020Brass Contributor
Bulk update Azure AD with user attributes from CSV
I am looking for a way to update user attributes (OfficePhone and Department) for about 500 users from a CSV to AzureAD using a powershell. Does anyone know of a script that I could use? I am new her...
- 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 # $FailedUsersGood luck!
Kind Regards, Manfred de Laat
carnelianfox
Mar 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,
vipfafen
Aug 18, 2021Copper Contributor
@arnelianfox did you get a working script? Can't get it to work either
- jebujohnAug 18, 2021Brass 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, 2021Brass 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 } }