Bulk rename AD users in multiple OU from CSV file

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Nga Huynh Nga Huynh 5 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37679
    Profile photo of Nga Huynh
    Nga Huynh
    Participant

    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 ([email protected]). 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.

    Example:
    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?

    Thanks

    #37680
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    I think you will also need to use Rename-ADobject

    #37704
    Profile photo of AK
    AK
    Participant

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

    #37719
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    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
    Profile photo of Nga Huynh
    Nga Huynh
    Participant

    Thank you Wei-Yen Tan. That did it.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.