Rename a domain joined computer with powershell

There’s a handful of reasons why you might need to change the name of a computer. Here’s something to get you started:

$newName = "NewName"
$oldName = gc env:computername
$Computer = Get-WmiObject Win32_ComputerSystem -ComputerName $oldName
$r = $Computer.Rename($newName, "Pa55w0rd", "DOMAIN\adminuser")
echo "Exit code: " $ret.ReturnValue
exit $r.ReturnValue

You’ll ideally fire a reboot after this has completed successfully.

If you’re running the script as a domain admin, you don’t need to specify credentials on the $Computer.Rename() function. I was running this as part of our software deployment process, which ran as the local system account, so needed to specify this info.

My code wasn’t always renaming systems, some failed with error code 1219. This is the error you get when trying to make multiple connections to a server using different credentials – The system hosting the software deployment was a domain controller, when logged on via that DC, it failed with the 1219. No biggie, as a reboot or two later, I would use another DC and the rename would happen fine.

Leave a Reply

Your email address will not be published. Required fields are marked *