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:
- Log on to the workstation.
- Access the control panel.
- Open the System applet.
- Select the change option to change the PC name or domain membership.
- Specify a new computer name.
- Click OK to save the change.
- Click OK on the main properties dialog.
- 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.
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:
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.]
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.