Renaming computers suck! Sure, there are worse things in life like famine, natural disasters, etc. Regardless though, the process is way more tedious than it should be. You have to remote in and kick off any logged on user. Even if you use Concurrent RDP, it is still a time consuming process. At least it was until PowerShell came along.
PowerShell 3.0 introduced the Rename-Computer cmdlet and made this task so much easier! Just to show you how easy, we are going to tackle three common tasks with this one cmdlet. We will:
- Remotely rename a single online computer
- Remotely rename multiple online computers
- Schedule renames for multiple computers (offline and online)
How to Remotely Rename a Computer with PowerShell
For most of this article, we will be using PowerShell ISE for our scripts. In the script pane (top console), type rename-computer and press F1. This is bring up the help file for the rename-computer cmdlet.*
You will notice that the only required parameter is -NewName. If you wanted to rename your local computer, you could type Rename-Computer NEWCOMPUTERNAME
Renaming a computer remotely with PowerShell is just as easy; all you’ll need is the -ComputerName parameter. Here is an example that renames a computer named PC1 to PC-1.
Rename-Computer -NewName PC-1 -ComputerName PC1
How to Remotely Rename Multiple Computers with PowerShell
In this example, we are going to use the Quest AD PowerShell cmdlets to export a list of computers to a CSV file. We are then going to edit the CSV file to contain our new computer names.
To export our current computer names, we can use this command:
get-qadcomputer -Name GA1116* | select-object Name | Export-Csv .\RenameComputers.csv -NoTypeInformation
Now, we have a CSV file containing all computers that start with GA1116. Open this file and create a second column with a header of NewName. Beneath this, fill in the corresponding new computer names.
Now let’s import this data into a new script and loop through each name change. Here is our script:
$Computers = import-csv .\RenameComputers.csv foreach ($Computer in $Computers){ Rename-Computer -NewName $Computer.NewName -ComputerName $Computer.Name }
If you receive any connection errors, ensure that all of the computers are turned on. If they aren’t, why not use that nifty Wake-On-LAN Active Directory option to wake them.
Schedule Renames for Multiple Computers
If you are renaming a large amount of computers (or working with machines that aren’t always connected), you will likely find the options above lacking. With a few modifications, you can have your computers rename themselves. With all of that spare time you now have, you can keep reading DeployHappiness. Doesn’t that sound like a win-win! Good, here is our script:
$Computers = import-csv .\RenameComputers.csv foreach ($Computer in $Computers){ Rename-Computer -NewName $Computer.NewName -ComputerName $Computer.Name -WhatIf if ($? -eq $True){ (get-content .\RenameComputers.csv) | Where-Object {$_ -notmatch $computer.name} | set-content .\RenameComputers.csv } }
Like our example above, this script relies on a CSV file containing our current and new computer names. The only real difference from above is the If statement. In PowerShell, $? stores the exit value of the previous command. So by saying if ($? -eq $True), we are saying “if the computer rename was successful.
If the rename was successful, we read the content of the CSV file and remove the newly renamed computer from the list. After running this script, only computers that weren’t successfully renamed will be left. By setting this script to run as a scheduled task, you can automate a large rename process with little effort! Of course, you will still want to ensure that you don’t accidentally create duplicate computer names.
* Isn’t that F1 shortcut key the coolest?
question… How would i add my domain credential so it does not prompt me for every computer? If anyone can show an example that would be awesome! I appreciate it in advance.
You would use something like this at the top of the script:
$Cred = get-credential
You would then use the $Cred variable whenever you need a credential.
Thanks for the script! Can I ask how I would go about restarting the computers that successfully changed names?
Sorry, should have been more specific.
I was referring to the “Schedule Renames for Multiple Computers” script.
Hey Greg – you could put a shutdown.exe -m command in your script or use the powershell cmdlet restart-computer.
Hi
awesome script but i need some more Intelligence in it
I need it to rename the computer
Delete old name from AD
and move new computer account to a specific container
if you can help me with this i will be over the moon over jupiter and all the way back
Are you moving all of the computers to the same OU?
Yes i am moving all the machines into a flat structure in AD
Will this rename only work in PowerShell 3.0? But the command is available on version 2.0. Will it work on version 2.0?
Where can I download a copy of PowerShell 3.0?
You can update to PowerShell 3.0 by using this link: http://www.microsoft.com/en-us/download/details.aspx?id=34595
If you are having issues you may want to use -DomainCredential Get-Credential. You can also create a variable for you credentials which may me helpful so it does not ask you every time you run the script.
Very true – thank you for the tip!