Renaming a user account

Sometimes we need to rename account – possibly because a user has changed their name.

$ou = "OU=BlogTests,DC=Manticore,DC=org"            
            
"`nMicrosoft"            
$name = "UserA"            
Get-ADUser -Identity $name |             
Rename-ADObject -NewName "UserAA"            
            
"`nAD provider"            
$name = "UserB"            
$dn = "cn=$name,$ou"            
Rename-Item -Path AD:\$dn  -NewName "cn=UserBB" -Force            
             
"`nQuest"            
$name = "UserC"            
Get-QADUser -Identity $name -SizeLimit 3000 |            
Rename-QADObject -NewName "UserCC"            
            
## works but thhrows an error            
"`nScript"            
$name = "UserD"            
$dn = "cn=$name,$ou"            
$newou = [adsi]"LDAP://$ou"            
            
$user = [adsi]"LDAP://$dn"            
$user.MoveTo($newou, "UserDD")


The cmdlets provide a cmdlet to rename AD objects



The provider uses Rename-Item. My testing shows that a Get-Item | Rename-Item structure will not work. The path has to be explicitly given to Rename-Item



The script uses the MoveTo method to move the object to the same OU but change its name during the move. An error message will be produced but the action is performed correctly

2 Responses to Renaming a user account

  • Gary Jackson says:

    Thanks for this Richard. I have a question regarding this. How would you rename all users in an OU to match the DisplayName for each OU.

    For example:

    Get-ADUser -SearchBase ‘ou=office365,dc=contoso,dc=com’ -Filter * -Properties displayname | % {Set-ADUser -Identity $_ -DisplayName $_.name}

    I have about 100 user accounts that have the proper Display Name as Last Name, First Name but the AD Object itself is the email address. I just need to do a Rename-ADOObject match the displayname for each user. I’ve tried this:

    Get-ADUser -SearchBase ‘ou=office365,dc=contoso,dc=com’ -Filter * -Properties displayname | Rename-ADOObject … .? I just don’t know the syntax here on how to pass the identity object down to the pipeline.

  • RichardSiddaway says:

Leave a Reply