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 –forceparameter 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:

 

$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.