To update the Employee ID for all users in Active Directory using PowerShell, you can use the Set-ADUser
cmdlet from the Active Directory module. This script assumes you have the necessary permissions to update user attributes in Active Directory and that you have the Active Directory module installed.
Here’s a step-by-step guide to create a PowerShell script that updates the Employee ID for all users:
1. Install the Active Directory Module
If you haven't already installed the Active Directory module, you can install it using the following command on Windows Server or Windows 10/11 with RSAT (Remote Server Administration Tools) installed:
Install-WindowsFeature RSAT-AD-PowerShell
On Windows 10/11, you can enable RSAT features:
Get-WindowsCapability -Name RSAT* -Online | Add-WindowsCapability -Online
2. Prepare the CSV File
Prepare a CSV file with the following format, where SamAccountName
is the user’s logon name, and EmployeeID
is the new employee ID you want to set:
SamAccountName,EmployeeID
jdoe,12345
asmith,67890
3. Create the PowerShell Script
Create a PowerShell script to read the CSV file and update the Employee ID for each user:
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the path to the CSV file
$csvFilePath = "C:\Path\To\EmployeeIDs.csv"
# Import the CSV file
$userData = Import-Csv -Path $csvFilePath
# Iterate over each row in the CSV file
foreach ($user in $userData) {
# Get the user's SamAccountName and EmployeeID
$samAccountName = $user.SamAccountName
$employeeID = $user.EmployeeID
# Update the EmployeeID attribute for the user
try {
Set-ADUser -Identity $samAccountName -EmployeeID $employeeID
Write-Host "Successfully updated EmployeeID for user: $samAccountName"
} catch {
Write-Host "Failed to update EmployeeID for user: $samAccountName. Error: $_"
}
}
4. Run the Script
Run the script in a PowerShell session with administrative privileges:
.\Update-EmployeeID.ps1
Notes:
- Permissions: Ensure the account running the script has the necessary permissions to modify user attributes in Active Directory.
- Error Handling: The script includes basic error handling to catch and display any errors that occur during the update process.
- Logging: You can enhance the script by adding logging to a file if needed.
Example with Enhanced Logging and Error Handling:
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the path to the CSV file
$csvFilePath = "C:\Path\To\EmployeeIDs.csv"
# Define the path to the log file
$logFilePath = "C:\Path\To\UpdateEmployeeID.log"
# Function to log messages
function Log-Message {
param (
[string]$message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$timestamp - $message"
Write-Host $logMessage
Add-Content -Path $logFilePath -Value $logMessage
}
# Import the CSV file
$userData = Import-Csv -Path $csvFilePath
# Iterate over each row in the CSV file
foreach ($user in $userData) {
# Get the user's SamAccountName and EmployeeID
$samAccountName = $user.SamAccountName
$employeeID = $user.EmployeeID
# Update the EmployeeID attribute for the user
try {
Set-ADUser -Identity $samAccountName -EmployeeID $employeeID
Log-Message "Successfully updated EmployeeID for user: $samAccountName"
} catch {
Log-Message "Failed to update EmployeeID for user: $samAccountName. Error: $_"
}
}
This enhanced script logs each success and failure to a log file, providing a record of the updates made and any errors encountered.
By following these steps and using the provided script, you should be able to update the Employee IDs for all your users in Active Directory.