Hi Nicholas Truden,
Thank you for posting in the Q&A Forums.
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the new employee ID value or a function to retrieve it (this is just an example value)
$newEmployeeID = "12345"
# Get all users in the domain (assuming your domain is example.com, replace with your actual domain)
$users = Get-ADUser -Filter * -SearchBase "DC=example,DC=com" -Properties extensionAttribute1
foreach ($user in $users) {
# Check if extensionAttribute1 already has a value or perform other checks based on your requirements
if ([string]::IsNullOrWhiteSpace($user.extensionAttribute1)) {
# If extensionAttribute1 is empty, set the new employee ID
Set-ADUser -Identity $user.SamAccountName -Replace @{extensionAttribute1=$newEmployeeID}
Write-Host "Updated employee ID for user: $($user.SamAccountName)"
} else {
# If extensionAttribute1 already has a value, you can choose to skip, overwrite, or perform other actions
# Here, we just print a message as an example
Write-Host "Skipping user: $($user.SamAccountName) because extensionAttribute1 already has a value."
}
}
Note:
- Please replace with your actual domain name.
example.com
- If your employee ID is not stored in , replace it with the correct attribute name.
extensionAttribute1
-
$newEmployeeID
should be a function or logic to generate or retrieve a new employee ID for each user. In the example above, I just set a fixed value for all users. - Depending on your requirements, you may need to add error handling and other logic.
- In large environments, this script may take some time to complete as it needs to iterate through all users and update them individually. Please consider the performance and potential impact when performing such operations.
Best regards
NeuviJ
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.