December 23, 2024

Powershell Script to update EmployeeID in AD

1 min read

Good morning, I was recently tasked with taking a spreadsheet with Employs in them and update them with 0’s at the beginning to ensure they are 8 digits.

In this script we need to Import in the CSV look up the user based on EmployeeID and then update the ID. I commented out a line of code you can use to test. A pause at the end to check if the ID changed. I could have put this into the script as well but I needed to write this quickly.

# Connect to Active Directory
Import-Module ActiveDirectory

# Get all users from Active Directory based on Employee ID

$Users = Import-Csv C:\Temp\EmpIDs.csv

# Loop through each user and update the employee ID
foreach ($user in $users) 
{
  $number = $user.EMPLOYEE
  Write-Host -ForegroundColor Red "Employee Number Before: $number"
  if ($number.Length -lt 8) 
    {
      $number = $user.EMPLOYEE
      $Newnumber = ('0' * (8 - $number.Length)) + $number
      $Identity = (Get-ADUser -Properties EmployeeID -Filter "EmployeeID -eq '$number'").SamAccountName
      Set-ADUser -identity $Identity -EmployeeID $Newnumber
      #Replace @{EmployeeID = $Newnumber}

    
    
      Write-Host -ForegroundColor Green "EmployeeID is now $newnumber"
      #Read-Host "Press Any Key to Continue"
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *