Have you ever had to rename a lab of computer or even an entire site? I have been checking computer names in Active Directory this week. Besides cleaning up stale computer accounts, I have been trying to ensure that all of my computer names are standardized. Today, I noticed that a lab of 36 computers was missing one computer.

What happened to N12?

When this lab was replaced in the summer, someone accidentally named a computer incorrectly. When I physically checked the lab, the computer at spot 12 was named 13. This caused every computer to be off by a name. Not wanting to login to 20+ computers and rename them manually, I turned to my trusty friend – PowerShell. Let’s see if the Rename-Computer cmdlet can give us a PowerShell script to change computer names!

I thought this was going to be as simple as running rename-computer against a CSV file. It turned out to be a little more complicated. When I ran just the Rename-Computer command, I received an error about existing computers on the domain. Let’s take an in depth look at this script.

First, we start by prompting for our administrative credentials. Next, we imported our CSV file which contained two columns: OldName and NewName. This will give us the information we need to work with.

To ensure that our newly (re)named computers are in the correct OU and security groups, we query them by using these three lines:

The actual renaming takes place between our two 5 second pauses. We remove the existing computer, rename it, and reboot it. Finally, we use another foreach statement to ensure our computers are in their original OU and security groups. That’s all there is to it! Now you can massively rename computers from your desk. As a word of caution, please test this script against a few machines first and ensure that your CSV is sorted by name.

Yes the script will automatically consider the first line as the header. You do not have to specify -header in the script if your csv file’s first line has the Oldname, NewName header. if you specify -header on the script then no need to have the oldname, newname on the csv file 🙂

When I created my .csv file I used “OLDNAME” and “NEWNAME” as headers. I discovered that the script was reading the first line and trying to connect to the machine “OLDNAME” that didn’t actually exsist. Once I removed the headers from the .csv things worked perfectly. Thanks again for a great article Joseph!