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
edl
Jan 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