Data Centers

How to rename a domain computer with Windows PowerShell

Use PowerShell to shave time off routine tasks like renaming computers. Here are some commands to try.

windows8serverscreeshot.png
Occasionally it may become necessary to rename a computer in an Active Directory environment. Using the System applet in the Control Panel is the way many administrators have been solving this issue for a long time. Until recently I didn’t think much about the work actually involved in changing a computer name. The typical steps are:

  1. Log on to the workstation.
  2. Access the control panel.
  3. Open the System applet.
  4. Select the change option to change the PC name or domain membership.
  5. Specify a new computer name.
  6. Click OK to save the change.
  7. Click OK on the main properties dialog.
  8. Restart the computer.

Depending on the number of things happening when the computer starts, going the long way (or through the Windows GUI) can take anywhere from 2 – 5 minutes. For one computer this may be acceptable, but for multiple PCs this could quickly become very tedious. Especially if you need to visit the computer to make these changes.

Enter WindowsPowerShell

Windows PowerShell is quite the helpful administrator’s tool. There are commands for a great many Windows functions without leaving the local machine. Once other tools are added, like Active Directory and Exchange, the capabilities of PowerShell go through the ceiling. In this case, we want to rename a computer using PowerShell. The way to do this using the rename-computer cmdlet is as follows: 

Rename-computer –computername “computer” –newname “newcomputername” –domaincredential domain\user –force –restart

Running this cmdlet and supplying the current computer name for –computername and the new computer name for –newname along with a user account that has permission to perform the function in Active Directory for the –domaincredential parameter.  Supplying the –force parameter ensures that the changes will be applied and –restart will force the computer to restart after the change is made, still a requirement for renaming computers.

This may seem like the same amount of effort as using the screens to change the name, but I assure you, even for one machine, it is faster. When I used this recently, I didn’t need to logon to complete the action and as soon as the command completed, the computer was rebooting. Since the client I renamed was not sitting in front of me, by the time I walked across my office to see if it was going to get renamed, the system was already shutting down as directed by the –restart switch.

Where this gets really handy is if you need to rename several computers. The cmdlet doesn’t really change all that much either, with the biggest difference being how the –computername and –newname parameters are handled. For example, to get this to work with multiple computers you might do something like this:

[UPDATE: Please note that this example has been changed to correct the previous version, which contained a looping error.]

$computers = Get-adcomputer | where {$_.name –like “sales-*”}
 
$num = 0
 
Foreach($computer in $computers)
 
{
 
For($num=1;$num –lt $computers.count;$num++)
 
{
 
Rename-computer –computername $computer –newname “s-$num” –domaincredential domain\user –force –restart
 
        }

Since the newname parameter is essentially incrementing the same name by one for up to the total number of computers this way will also allow a fairly quick renaming of computers.  Unless your environment rolls PCs over frequently, I would imagine the single cmdlet would be of most use with the specified computername and newname.

There are many other ways to tackle this problem, although you will likely need lists or loops of some kind to plow through the items. You might choose to populate your computers list another way, maybe by getting the names of existing systems from Active Directory:

Note: Appending the –whatif switch on the Rename-computer cmdlet will display what the command would do if it actually executed. This can give you an idea of the performance without actually changing the computer name.

More PowerShell tips:

PowerShell is a very powerful and helpful tool to add to any Windows administrators arsenal of day to day tools. Using it to rename computers is much faster than accomplishing the same task through the traditional method of using the control panel. The more computers you need to rename, the more value you will see from allowing PowerShell to do the lifting.



About

Derek Schauland has been tinkering with Windows systems since 1997. He has supported Windows NT 4, worked phone support for an ISP, and is currently the IT Manager for a manufacturing company in Wisconsin.

5 comments
jhmeader
jhmeader

Is there a way to do this where the name change will be processed on next reboot? I'm investigating changing our naming convention here, but I don't want to force the machines to restart. I'm a Mac/Linux guy, so PowerShell is slightly foreign to me, so please forgive any ignorance. Thanks!

Derek Schauland
Derek Schauland

The original first example was incorrect, and contained a looping error.  When originally testing this it was working as needed, however when testing was expanded, the inner loop for new names ran through all iterations each time.  The example was removed because of this.  Thank you for bringing this to my attention so we could get this cleaned up.

darksidegeek
darksidegeek

As was already pointed out, the 1st example is wrong. The proper way to do this is to have a file with *pairs* of names. Loop on the lines and split each by the delimiter into old and new names. No inner loop. Bonus: you don't run into situations where the filenames may not have a matching number of systems.

The 2nd is equally as incorrect, renaming the computer 5 times. Here, the proper way is to use a global incremented value and again ditch the inner loop. Yet ham-fistedly, I believe it may actually work (assuming there are only 5 systems in Sales)! The 1st computer will ultimately end up as "s-5". But AD won't allow a duplicate "s-5" PC, so the 2nd will succeed at "s-4" but fail on "s-5", and so on.

Clearly the author forgot how loops work and made no attempt to test either example. Sorry to be so harsh, but this was very amateurish.

Jimmy S
Jimmy S

I don't know power shell at all so please forgive me if I have mis-understood this, but in the first example with an outer ($computers) and an inner ($newnames) loop wouldn't the inner loop (i.e. the rename) be executed for every element in $newnames for each element of $computers in turn?

RU7
RU7

Issue with first solution.

Each time through the first FOR loop, the current computer to be renamed is renamed and restarted for each entry in the new name file.  If there are 100 entries in each file, each computer will be renamed 100 times and all computers will end up with the same name, the last one in the new names list.  That's a BAAAAAD thing.

Editor's Picks