Bulk rename AD users in multiple OU from CSV file

I need to rename all domain accounts (samaccountname) and UPN with the data I will be provided with an excel (.csv) spreadsheet. SamAccount UPN prefix and name will be the same (xyz123@company.com). I am a newbie powershell script and not familiar with PS syntax.

I created a .csv file titled “UserList.csv” within the same folder as my .ps1 script. Within the .csv file, I have 3 columns: UserID,sAMAccountName, UserPrincipalName.

The UserID column has the user logon names for the users. The sAMAccountName and UserPrincipalName columns have what the company would like the new user logons names to be.

I want to use Get-ADuser cmlet to export a list of all Domain users to CSV file and then try Set-ADuser after.

Import-Module ActiveDirectory
$UserList = Import-CSV C:\scripts\UserList.csv
ForEach ($User in UserList)
Get-ADUser -Filter “sAMAccountName -eq ‘$($User.UserID)’”
-SearchBase ‘OU=Users,DC=atciso,DC=lmco,DC=com’

Am I on the right track?


I think you will also need to use Rename-ADobject

Set-Aduser is what you are after.
$userupdate = Get-ADUser -Identity $User.SamAcccountName
$userupdate.SAMAccountName = $User.NEWSamAcccountName, etc.
Set-ADUser -Instance $userupdate

I think this will work. You might need to tweak:

$UserList = Import-CSV C:\scripts\UserList.csv

ForEach ($User in $UserList)
    $csvusername = $user.samaccountname
    $csvuserprincipalname = $user.userprincipalname
    Get-ADUser $($user.Userid) | Set-Aduser -SamAccountName $csvusername -      
    UserPrincipalName $csvuserprincipalname

Thank you Wei-Yen Tan. That did it.