SOLVED

Bulk update Azure AD with user attributes from CSV

Copper Contributor

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 here and if I have not given enough information, please let me know. I tried using Set-AzureADUser piping records using a foreach statement from a csv that I imported, but it was throwing up errors. 

Thanks!

Jacob

68 Replies

@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,

@Manfred101 This worked great!

One think im stuggling with, is the user doesnt get updated when one of the properties is empty? is there a way around that? i.e. the mobile number for that user is empty in the csv most of the update for that user fails.

@esfitzI got around this by entering a - (dash) into blank fields. Otherwise, you are right, the script will stop there.

@wllrkn Thanks bunch, removing this fixed the issue for me :). 

It's great work thanks for teaching us.

I have one question

How get all attributes in azure if I want to update deferent value all job information.

Kind regards
@arnelianfox did you get a working script? Can't get it to work either

@vipfafen 

 

$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
  }
 }

 

Thanks 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>"
...
Try 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
Finaly it Woks! Thank you @jebujohn

@jebujohn 

This is the shortest line to get the job done:
Assuming your csv file has two columns;; UserPrincipalName and a column with the attribute you wish to update ( the user's Title for example); assuming the file is stored in the D drive and is titled updateusers, then here's you code: 
Import-CSV D:\updateusers.csv | ForEach-Object { Set-AzureADUser -ObjectID $_.UserPrincipalName -Title $_.Title 

If you want to update more attributes you can add them to the code, for example to update department just add -Department $_.Department to the code above: 

Import-CSV D:\updateusers.csv | ForEach-Object { Set-AzureADUser -ObjectID $_.UserPrincipalName -Title $_.Title -Department $_.Department 

Agree that code is the simple I just like some feedback on one's that did not work - however things get more challenging with updating the manager which was the last set of comments

Hi @Manfred101 

 

How about to update Company Name using CSV?

@Tamerhalawah 

i did it like That: 

 

# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
$upn = $CSVrecord.UserPrincipalName
$upnmanager = $CSVrecord.Manager
$user = Get-AzureADUser -Filter "UserPrincipalName eq '$upn'"
$Manager = Get-AzureADUser -Filter "UserPrincipalName eq '$upnmanager'"
if ($user) {
try{
$user | Set-AzureADUser -City $CSVrecord.City -CompanyName $CSVrecord.CompanyName -Department $CSVrecord.Department -StreetAddress $CSVrecord.StreetAddress -PostalCode $CSVrecord.PostalCode -JobTitle $CSVrecord.JobTitle -Mobile $CSVrecord.Mobile -PhysicalDeliveryOfficeName $CSVrecord.PhysicalDeliveryOfficeName
Set-AzureADUserManager -ObjectId $user.ObjectId -RefObjectId $Manager.objectId
} catch {
$FailedUsers += $upn
Write-Warning "$upn user found, but FAILED to update."
}
}
else {
Write-Warning "$upn not found, skipped"
$SkippedUsers += $upn
}
}

 

Thanks au lot to all of you ! 

@Manfred101 

 

Hi Manfred, How about if I need to update the Manager field?

@Noufal86 

you should add a manager field in our CSV with thier UPN

and add these on the loop

 

$upnmanager = $CSVrecord.Manager
$Manager = Get-AzureADUser -Filter "UserPrincipalName eq '$upnmanager'"
if ($user) {
try{
Set-AzureADUserManager -ObjectId $user.ObjectId -RefObjectId $Manager.objectId

}

@Manfred101  - The below post shares the PowerShell script to modify bulk user attributes for multiple user accounts in a simple way by importing user details from a CSV file.

 

https://morgantechspace.com/2022/03/update-bulk-azure-ad-user-attributes-using-powershell.html

 

This script helps to update bulk user attributes as hash table in single command execution.

 

#Hashtable to keep multiple attribute values
$AttributesToUpdate = @{}

$AttributesToUpdate["JobTitle"] = "Sales Manager"
$AttributesToUpdate["Department"] = "Sales"

# Set required user attributes.
# Need to prefix the variable AttributesToUpdate with @ symbol instead of $ to pass hashtable as parameters (ex: @AttributesToUpdate).
Set-AzureADUser -ObjectId "user@domain.com" @AttributesToUpdate

# Refer to the below post for more details.
# https://morgantechspace.com/2022/03/update-bulk-azure-ad-user-attributes-using-powershell.html

 

 

Hello Everyone I need your help
Thanks in advenced
I need to Add and Update profile user in Azur with powershell , I need to add an atributs IP PHONE in user profile in Azure by Script
This is almost perfect, I assumed I would just use this and substituted the attribute as needed. I am looking to update OfficeLocation

my csv is like
userPrincipalName;OfficeLocation
email address removed for privacy reasons;HQ

I updated line 17 in the script to Update-AzureADUser -OfficeLocation $CSVrecord.OfficeLocation

aaaand it fails to update.

Your assistance is greatly appreciated.

@Manfred101 Thanks for this. I know this is old but, if you are still here, how long should it take to show up in Azure AD.