Renaming AD accounts

Berkani49 41 Reputation points
2020-11-27T04:52:30.963+00:00

Hello,
I’m looking for a powershell script that let me rename the SamaccountName and UPN of a list of users from csv file.
Exemple:
SamaccountName: USA.A1234 to A1234
And UPN to A1234@keyman instead of USA.A1234@keyman

Thank you

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,524 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andy David - MVP 147.9K Reputation points MVP
    2020-11-27T14:23:05.333+00:00
    import-csv users.csv | %{set-aduser $_.SamAccountName -SamAccountName $_.NewSamAccountName -UserPrincipalName  $_.NewUPN}
    

    Test with a small number of items first, then you can run in bulk against the bigger list

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ian Xue 36,751 Reputation points Microsoft Vendor
    2020-11-27T06:04:27.163+00:00

    Hi,

    What is your csv file like? You can use Set-ADUser to set the attributes of an AD user.

    Get-ADUser -Filter {SamAccountName -eq "USA.A1234"} | Set-ADUser -SamAccountName "A1234" -UserPrincipalName "A1234@domain"  
    

    For more details you can refer to this link
    https://learn.microsoft.com/en-us/powershell/module/addsadministration/set-aduser?view=win10-ps

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Berkani49 41 Reputation points
    2020-11-27T14:03:25.327+00:00

    Hello,

    thank you for the response, my csv file looks like:

    SamAccountName,NewSamAccountName,NewUPN

    I have 300 users to process

    Thank you

    0 comments No comments

  3. Berkani49 41 Reputation points
    2020-11-27T16:12:10.613+00:00

    Thank you very much, I just tested with few accounts and it works.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.