<#
.SYNOPSIS
.\Set-EmployeeID.ps1
.DESCRIPTION
The script will set/update the employee ID for Microsoft 365 users.
.LINK
o365info.com/set-employee-id-microsoft-365
.NOTES
Written By: o365info
Website: o365info.com
.CHANGELOG
V1.00, 06/01/2023 - Initial version
#>
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Read the CSV file
$users = Import-Csv -Path "C:\temp\empcode.csv"
# 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'." -ForegroundColor Cyan
}
else {
# Update the EmployeeId
Update-MgUser -UserId $userPrincipalName -EmployeeId $employeeId
Write-Host "User '$userPrincipalName' updated Employee ID '$employeeId'successfully." -ForegroundColor Green
}
}
else {
# User not found
Write-Host "User '$userPrincipalName' not found and couldn't set Employee ID $employeeId." -ForegroundColor Red
}
}
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article