There are a dozen ways for you to rename a computer on a domain. The PowerShell script, below, is a fun way to take care of a few misnamed machines efficiently. If you wanted, you could even embed it within your AD management tools to make the whole process a one-click affair.
It starts by prompting for your domain credentials. It then enters a While loop. I really enjoy the simplicity of using While ($true) as it creates a forever loop.
You will then be prompted for the old/current computer name, new computer name, and if you would like to restart the machine. Once the machine is renamed, it will automatically repeat the process. The old/current computer does have to be turned on and on the domain. Of course, you do need permission to rename the computer.
When it comes to renaming computers, your methods now equal a baker’s dozen. As everyone knows – that is the best type of dozen. If you have any questions or suggestions, leave a comment below!
$Credential = Get-Credential -Message User: username@DOMAIN or Domain\Username While ($true){ write-host "" $OldComputerName = Read-Host "What is the old computer name?" $NewComputerName = Read-Host "What is the new computer name?" $Restart = Read-Host "Restart the computer? Y for yes or N for no." if ($Restart -eq "Y"){ Rename-Computer -ComputerName $OldComputerName -NewName $NewComputerName -Restart -Force -DomainCredential $Credential -verbose } if ($Restart -ne "Y"){ Rename-Computer -ComputerName $OldComputerName -NewName $NewComputerName -Force -DomainCredential $Credential -verbose } }$Credential = Get-Credential -Message User: username@DOMAIN or Domain\Username While ($true){ write-host "" $OldComputerName = Read-Host "What is the old computer name?" $NewComputerName = Read-Host "What is the new computer name?" $Restart = Read-Host "Restart the computer? Y for yes or N for no." if ($Restart -eq "Y"){ Rename-Computer -ComputerName $OldComputerName -NewName $NewComputerName -Restart -Force -DomainCredential $Credential -verbose } if ($Restart -ne "Y"){ Rename-Computer -ComputerName $OldComputerName -NewName $NewComputerName -Force -DomainCredential $Credential -verbose } }
I made a similar script but i added a test-connection function to ensure the pc was online and provide a dialog to the user with “PC online” or “PC offline”. This way lower skilled employees dont need to translate powershell errors if the pc isnt online and they get an error message.
Thanks for you site, I’m learning a lot.
I don’t understant why you enter in forever loop. I mean what if I need cancel process.
The forever loop is not necessary but it is a fun thing to know about. I got tired of confirming that I needed to rename another machine. When I needed to cancel, I would just terminate with CTRL + C.