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 # $FailedUsers
Good luck!
Kind Regards, Manfred de Laat
Manfred101
May 08, 2020Iron Contributor
Hello Jacob,
Your CSV has to look something like this:
UserPrincipalName;Department;TelephoneNumber
manfreddelaat@domain.nl;IT;0135113333
manfred@domain.nl;IT;0622222222
Your 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
- edlJan 08, 2025Copper Contributor
this script doesn't work anymore
- jebujohn1Jan 08, 2025Copper Contributor
Use Microsoft Graph instead. Here is an example for the EmployeeID (o365info.com)
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Read the CSV file
$users = Import-Csv -Path "set your path here"
# Go through each user in the CSV and update the EmployeeId property
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
$employeeId = $user.EmployeeId
# Check if the user exists
$existingUser = Get-MgUser -UserId $userPrincipalName -Property UserPrincipalName, EmployeeId | Select-Object UserPrincipalName, EmployeeId -ErrorAction SilentlyContinue
if ($existingUser) {
# Check if the existing EmployeeId matches the new value
if ($existingUser.EmployeeId -eq $employeeId) {
# EmployeeId already set with the same value
Write-Host "User '$userPrincipalName' already has Employee ID '$employeeId'."
}
else {
# Update the EmployeeId
Update-MgUser -UserId $userPrincipalName -EmployeeId $employeeId
Write-Host "User '$userPrincipalName' updated Employee ID '$employeeId'successfully."
}
}
else {
# User not found
Write-Host "User '$userPrincipalName' not found and couldn't set Employee ID $employeeId."
}
}
- Jota365Feb 19, 2025Copper Contributor
- DeletedAug 22, 2024Thank you so much Manfred. Thank you so much.
- AyushDwivedi1610Jan 16, 2024
Microsoft
Manfred101 Could you let me know the performance of this code, if I am trying to update 5000 records, typically how much time will this take?
- Phani1615Dec 14, 2023Copper Contributor
Similarly for updating the office location of users in Azure AD in bulk I've tried updating the above script as mentioned below (In CSV file taken values with columns userPrincipalName and officeLocation)
foreach ($CSVrecord in $CSVrecords) {
$upn = $CSVrecord.userPrincipalName
$user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"
if ($user) {
try{
$user | Set-AzureADUser -officeLocation $CSVrecord.officeLocation
} catch {
$FailedUsers += $upn
Write-Warning "$upn user found, but FAILED to update."
}
}
else {
Write-Warning "$upn not found, skipped"
$SkippedUsers += $upn
}
}But all the users were getting skipped instead of office location being populated for the users, as per the csv file with warning:
WARNING: user found, but FAILED to update.Requesting your help
- Mthielen525Jan 15, 2023Copper Contributor
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.
- Wim_GroffilsJan 17, 2023Brass Contributorpretty fast, for me its about a minute
- JawanLJul 18, 2022Copper ContributorThis 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.- Wim_GroffilsJan 17, 2023Brass ContributorWatch out for the separator in your CSV file what error do you get?
- Kevin_MorganApr 22, 2022Iron Contributor
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
- Noufal86Apr 13, 2022Copper Contributor
- BucheronPWApr 13, 2022Copper Contributor
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}
- rinianggrainiNov 18, 2021Copper Contributor
- Abdaqel335Jun 10, 2021Copper ContributorIt'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