Update AD EmployeeNumber Attribute From CSV File
Wednesday, June 4th, 20142014-06-04 Initial Post
The other month I needed to update the EmployeeNumber attribute of all Active Directory user accounts based on a CSV file generated from Oracle HRMS. You may modify this script to update a different attribute, such as EmployeeID.
#Script updated 2014-03-31, created 2014-03-06, by SysAdmin-E.com. #PURPOSE: Takes a CSV input file that contains "E-mailAddress,EmployeeNumber" on each line (DO NOT include header in CSV file). # Searches AD for user account that has the e-mail address as its primary address. # If found, update that respective AD account with the corresponding EmployeeNumber. #There is some limited error checking and a status file is generated after script completion. #I was not able to get the Import-Csv command to work correctly so I used Get-Content to grab data from the input file. #Tested against Windows Server 2008 R2 SP1 AD. $strFilePathInput = "X:\TempPowerShell\setAD-EmployeeNumberINPUT.csv" $strFilePathOutput = "X:\TempPowerShell\setAD-EmployeeNumberOUTPUT.csv" Get-Content $strFilePathInput | ForEach-Object{ $strUserInfoLineFromFile = $_ $strUserEmailAddress = $strUserInfoLineFromFile.Split(",")[0] $strUserEmployeeNumber = $strUserInfoLineFromFile.Split(",")[1] If($objADuser = Get-ADUser -Filter {mail -eq $strUserEmailAddress }) #The mail attribute is the primary/reply e-mail address. { Set-ADUser $objADuser -EmployeeNumber $strUserEmployeeNumber If($?) #If True, which means previous command (set EmployeeNumber) was successful. Invalid e-mail address or insufficient AD permissions will make this False. { "OK - Updated," + $strUserInfoLineFromFile | Out-File -Append $strFilePathOutput } Else { "ERROR - Could not update," + $strUserInfoLineFromFile | Out-File -Append $strFilePathOutput } } Else { "E-mail address has no primary match," + $strUserInfoLineFromFile | Out-File -Append $strFilePathOutput } } Exit