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