Bulk rename AD users in multiple OU from CSV file

This topic contains 4 replies, has 4 voices, and was last updated by  Nga Huynh 2 years, 1 month ago.

  • Author
  • #37679

    Nga Huynh

    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?


  • #37680

    Wilfredo Perez

    I think you will also need to use Rename-ADobject

  • #37704


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

  • #37719

    Wei-Yen Tan

    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
  • #37773

    Nga Huynh

    Thank you Wei-Yen Tan. That did it.

You must be logged in to reply to this topic.