Use PowerShell to Remotely Rename Active Directory Computers

If you have your machines bound to an Active Directory domain, renaming computers is a snap thanks to the awesome power of PowerShell.

Renaming One Computer

Use the following command to remotely rename a computer:

Rename-Computer -ComputerName <OldName> -NewName <NewName> -DomainCredential <Domain\Username>

Some other useful switches:

-Force Forces the command to run without asking for user confirmation
-Restart Forces the computer to restart after the change is made
-PassThru Returns the results of the command, otherwise no output is generated

An example to force a computer named Machine1 to change to Machine2 and then immediately restart:

PowerShellRenameComp

Keep in mind that if the -restart switch is not used then the changes will not take effect until after the client computer is restarted.

Renaming Multiple Computers

For the sake of this example, we’ll just be using two computers but this technique can be particularly useful when needing to rename a large number of computers.

First create a comma separated values (.CSV) file in the format of: OldName, NewName

This can be done with a simple text editor like Notepad, just save as a .CSV file extension.  I’ll specify a computer called Machine1 be renamed to Machine2 and another named Machine3 be renamed to Machine4 saved in a file called Comps.CSV:

CompsCSV

Next I’ll use two PowerShell commands.  The first is to import my .CSV file into a variable I’ll call $Comps:

$Comps = Import-Csv C:\Users\Q\Desktop\Comps.csv -Header OldName, NewName

The second command loops through the .CSV, renaming each computer and forcing them to restart:

Foreach ( $Server in $Comps ) {Rename-Computer -ComputerName $Server.OldName -NewName $Server.NewName -DomainCredential AD\Qname -Force -Restart}
Advertisements

2 comments

  1. Robin Estes · · Reply

    What happens if some computers listed on the CSV file are offline. will the script move on to the next line?

    Like

    1. Yes, if a client is offline then Rename-Computer will attempt to run which will fail. It will then continue on to the next iteration of the for loop (the next line in the CSV). If needed you can utilize wake-on-LAN to wake up machines over the network, assuming yours allows it.

      Good question, Robin, thank you for asking.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: