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.
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!
$Cred= Get-Credential $CSV = Import-Csv "U:\LAB.csv" -Header OldName, NewName Foreach ($name in $CSV) { $ComputerName = Get-QADComputer $Name.NewName $GroupMemberships = Get-QADComputer $Name.NewName | Get-QADMemberOf $OUMembership = Get-QADComputer $Name.NewName | Select-Object ParentContainer Remove-QADObject -Identity $ComputerName Start-Sleep 5 Rename-Computer -ComputerName $name.OldName -NewName $name.NewName -DomainCredential $Cred -Force -Restart Start-Sleep 5 foreach ($GroupMembership in $GroupMemberships) {Add-QADMemberOf -Group $GroupMembership -Identity $ComputerName } Move-QADObject -Identity $ComputerName -NewParentContainer $OUMembership.ParentContainer }
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:
$ComputerName = Get-QADComputer $Name.NewName $GroupMemberships = Get-QADComputer $Name.NewName | Get-QADMemberOf $OUMembership = Get-QADComputer $Name.NewName | Select-Object ParentContainer
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.
You can then use this script to make sure you didn’t miss a computer or mess up a name.
To optimize this process even more, you could choose to delete the old dns record at the same time.
Have a look here: https://technet.microsoft.com/en-us/library/jj649872.aspx?f=255&MSPPError=-2147217396
Great tip, Jonas! I’m always down for some optimization.
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 🙂
Thanks for the tip Riaz! I always appreciate some optimization!
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!
Thank you for sharing this tip Jonathan!