PowerShell Script to Update AD attribute from CSV

Recently I’ve been working on staff account creation automation and wanted to use employee IDs as the unique identifier. So before I could unleash my script to create accounts I needed to populate all existing users with their IDs. Here is a script I wrote that can be easily adjusted to update other AD attributes as well.

This will look at the sAMAccountName and if it exists, update the employeeID attribute.

Import-Module ActiveDirectory

$csvFilePath = "\\scripter\FTP\TylerIV Export\DL\employeeidupdate.csv"  # Modify this to the correct path of your CSV file
$employeeIdColumn = "Employee ID"  # Modify this if the employee ID column has a different name
$samAccountNameColumn = "sAMAccountName"  # Modify this if the SAM account name column has a different name

$users = Import-Csv $csvFilePath

foreach ($user in $users) {
    $employeeId = $user.$employeeIdColumn
    $samAccountName = $user.$samAccountNameColumn
    $adUser = Get-ADUser -Filter "sAMAccountName -eq '$samAccountName'" -Properties EmployeeID
    
    if ($adUser) {
        # Update employeeID attribute
        Set-ADUser -Identity $adUser.SamAccountName -EmployeeID $employeeId
        Write-Host "Updated employeeID for user $($adUser.SamAccountName)"
    } else {
        Write-Warning "Could not find user with sAMAccountName '$samAccountName'"
    }
}

Sample on how to format the csv

Employee ID,sAMAccountName
1111,Cory.Fiala
1234,Dawn.Seeder
5999,Tony.Stark

Leave a Comment

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

Scroll to Top