PowerShell: Rename an Active Directory User

March 11th, 2017 by and tagged , , ,

This came up at work the other day. Another admin had attempted to rename an AD User account and it had only partially gotten renamed — the SAM Account, Name and Display name were all correct, but the old user name was still showing up in a couple of places, including the login screen. The user was not happy, so I was asked to fix it, and provide a script that would handle it correctly. I poked around a bit and found the issue – even if you set all of the obvious properties correctly (and the other admin had missed UPN), it still won’t show correctly on that logon screen — you need to actually rename the AD object itself. So, after I fixed the problem user’s account, I wrote up a script to solve the problem for the next time. I chose to use a CSV file as the input, but you could easily re-work this to work off either a CSV file or a set of command-line parameters. But honestly, I don’t ever want to have to enter that many command-line parameters for a simple script. Especially if I have more than one to change.


The script uses Get-ADUser with the old name, then pipes it to Set-ADUser, and finally pipes it to Rename-ADObject to finish the process.  I even gave it basic help. :)

Renames the Active Directory users
Rename-myADUser reads a CSV file to identify an array of users. The users are then 
renamed to the new name in Active Directory.
Renames the AD Accounts of the users in the default "ADUsers.csv" source file
Rename-myADUser -Path "C:\temp\ChangedUsers.txt"
Renames the AD accounts of the users listed in the file C:\temp\ChangedUsers.txt"
.Parameter Path
The path to the input CSV file of format:

The default value is ".\ADUsers.csv".  
    Author: Charlie Russel
 Copyright: 2017 by Charlie Russel
          : Permission to use is granted but attribution is appreciated
   Initial: 03/09/2017 (cpr)
     $Path = ".\ADUsers.csv" 

$ADUsers = @()
If (Test-Path $Path ) {
   $ADUsers = Import-CSV $Path
} else { 
   Throw  "This script requires a CSV file with user names and properties."
$PDC = (Get-ADDomain).PDCEmulator
Write-Verbose "The PDC Emulator has been identified as $PDC"
Write-Verbose " "

ForEach ($User in $ADUsers ) {
   Write-Verbose "Modifying $user.OldSam to $user.NewName" 
   Sleep 3
   Get-ADUser -Identity $User.OldSam -Properties * | `
   Set-ADUser -Server $PDC `
              -DisplayName $user.DisplayName `
              -EmailAddress $User.EmailAddress `
              -SamAccountName $User.SamAccountName `
              -GivenName $User.GivenName `
              -Surname $User.Surname `
              -UserPrincipalName $user.UserPrincipalName `
              -PassThru | `
   Rename-ADObject -NewName $user.NewName -Server $PDC -PassThru


Posted in Active Directory, PowerShell | 2 Comments »

2 Responses to “PowerShell: Rename an Active Directory User”

  1.   subbuganesh Says:


    Please help me to solve the below error message.

    Set-ADUser : replace
    At C:\Users\XXXX\Documents\AD_USER-RENAME\AD_user_rename.ps1:49 char:4
    + Set-ADUser -Server $PDC `
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (CN=rnd,CN=Users,DC=AB,DC=in:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser