Update Employee ID for Bulk Azure AD Users using PowerShell

In this blog, we are going to explore how to add/update the EmployeeId attribute in Azure AD for Microsoft Office 365 users in bulk using PowerShell. The EmployeeId field is populated as Extension property (Additional properties) in Azure AD.

We can use the Set-AzureADUser cmdlet to update the normal Azure AD user properties. But we need to use the Set-AzureADUserExtension cmdlet to update a user extension property. Before you start, run the following command to connect the Azure AD PowerShell module.

Connect-AzureAD

Run the following command to set the EmployeeId attribute value for a single Microsoft 365 user.

Set-AzureADUserExtension -ObjectId "[email protected]" -ExtensionName "employeeId" -ExtensionValue "MTS-A1-61"

We can use the Get-AzureADUserExtension cmdlet to retrieve an Azure AD user’s extension attribute.

$UserExtProperties = Get-AzureADUserExtension -ObjectId "[email protected]"
$UserExtProperties["employeeId"]

The EmployeeId property value can also be retrieved through the Get-AzureADUser cmdlet.

$UserExtProperties = Get-AzureADUser -ObjectId "[email protected]" | Select -ExpandProperty ExtensionProperty
$UserExtProperties["employeeId"]

Update Employee ID Property for Bulk Microsoft 365 Users from CSV

To update users in bulk, we can keep the required user details in a CSV file. Consider the CSV file “AzureADUserEmployeeIds.csv” (Download sample CSV) which holds the user details in each row with the column headers UserPrincipalName and EmployeeId. The below script imports the user details from the CSV file and set the EmployeeId value in Azure AD for users one by one.

#Read user details from the CSV file
$AzureADUsers = Import-CSV "C:\Temp\AzureADUserEmployeeIds.csv"
$i = 0;
$TotalRows = $AzureADUsers.Count

#Array to add add status result
$UpdateResult=@()

#Iterate users and set employeeId attribute value one by one
Foreach($UserInfo in $AzureADUsers)
{
$UserId = $UserInfo.'UserPrincipalName'
$EmployeeId = $UserInfo.'EmployeeId'

$i++;
Write-Progress -activity "Processing $UserId " -status "$i out of $TotalRows completed"

try
{
#Set the employeeId attribute value
Set-AzureADUserExtension -ObjectId $UserId -ExtensionName "employeeId" -ExtensionValue $EmployeeId
$UpdateStatus = "Success"
}
catch
{
$UpdateStatus = "Failed: $_"
}

#Add update result status
$UpdateResult += New-Object PSObject -property $([ordered]@{
User = $UserId
Status = $UpdateStatus
})
}

#Display the update status result
$UpdateResult | Select User,Status

#Export the update status report to CSV file
#$UpdateResult | Export-CSV "C:\Temp\UpdateEmployeeIdStatus.CSV" -NoTypeInformation -Encoding UTF8
Advertisement