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
pretty fast, for me its about a minute
Watch out for the separator in your CSV file what error do you get?
@Manfred101. Any idea how to set a field to null?

@dnelsonpd 

 

Hi, You have to use the ExtensionProperty to set the null value or clear the property with Set-AzureADUser cmdlet.

 

$properties = [Collections.Generic.Dictionary[[String],[String]]]::new()
$properties.Add("Mobile", [NullString]::Value)
Set-AzureADUser -ObjectId "email address removed for privacy reasons" -ExtensionProperty $properties

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

 

Check this post for more details: Remove or Clear Property or Set Null value using Set-AzureADUser cmdlet 

so any other set attribute does not get touched at all, correct? I"m only looking to update Titles and plan on testing this but obv don't want anything else to change on the users. . .

# 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 -JobTitle $CSVrecord.JobTitle
} 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

@Manfred101 

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 

Hello @JawanL, were you able to update the office location by making changes to the above script, I'm also trying to do the same, so wondering whether you were able to achieve it. If you have, please share the changes you have made.
Simple to see and learn this PowerShell code start with exchangeonline first after that we go through AzureAD: Please email me: email address removed for privacy reasons :)

Import-Module ExchangeOnlineManagement
Install-Module ExchangeOnlineManagement
Connect-ExchangeOnline
$CSVrecords = Import-Csv C:\newdata.csv
$total = $CSVrecords.Count
$i = 0
$newdom = "your.domian.com" #type vefied domain from your domain list
foreach ($user in $CSVrecords)
{
$i = $i + 1
$oid = $user.ObjectId
$username = $user.GivenName.ToLower() + "." + $user.Surname.ToLower() #ToLower is to create all character in lowercase ToUpper() for uppercase
#Write-Host "$username"
$finalemail = $username + "@" + $newdom
#Write-Host "$finalemail"
Set-Mailbox $oid -EmailAddresses $finalemail
Write-Progress -activity "Processing $user.DisplayName $i out of $total completed"

}
Write-Host "All UPN set to domain $newdom"

Now Again time for AzureAD

Install-Module AzureAD
import-Module AzureAD
Connect-AzureAD
$CSVrecords = Import-Csv C:\Users\aruns\OneDrive\Desktop\newdata.csv
$total = $CSVrecords.count
$i = 0
$pdomain = "your.domain.com" #New tetantdoamin here betweeen " "
# Loop trough CSV records
foreach ($user in $CSVrecords)
{
$i = $i + 1
$oid = $user.ObjectId
$username = $user.GivenName.ToLower() + "." + $user.Surname.ToLower()
#Write-Host "$username"
$finalemail = $username + "@" + $newdom
#Write-Host "$finalemail"
Set-AzureADUser -ObjectId $oid -UserPrincipalName $finalemail
Write-Progress -activity "Processing $user.DisplayName $i out of $total completed"
}
Write-Host "Well done Action Completed!"

If helpful I love you all

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