Rename Computer PowerShell 3.0

Rename a Windows Machine Name with PowerShell v3Rename-Computer PowerShell 3.0

The purpose of this page is to give you basic examples of a new PowerShell 3 cmdlet called Rename-Computer.

 ♦

A Simple Example of PowerShell Renaming Computer

This cmdlet works equally well with workstations or servers, and as we will see later, PowerShell 3.0 can automate renaming of a list of computers from a .CSV file.

# PowerShell 3.0 Renames a Computer
Rename-Computer -NewName "Windows8"

Note 1: PowerShell expects the new hostname to be in the first position after the Verb-Noun, thus you could simplify the instruction to:
Rename-Computer "Windows8", in this case the -NewName parameter would be assumed.

Note 2: Alternatively, you could explicitly add the ComputerName parameter.  The benefit would be to learn more about this cmdlet, perhaps with a view to creating a script to rename lots of computers.  See in the next example:

Comparison of Control Panel GUI with Rename-Computer Cmdlet

Rename-Computer PowerShell 3.0 

I am a great advocate of learning PowerShell by finding the Windows GUI that corresponds to the current script.  In this instance The ‘System’ screen above alerts me to the need for a reboot before the renaming takes affect.  This is because the Application and Network services read the computer name only when they start.  Armed with this additional information we will modify the script in the next example.

PowerShell Script to Rename a Computer

You may have realized the need to restart the computer before the new name becomes visible to other computers on the network; fortunately, Rename-Computer has a -restart parameter.

# PowerShell 3.0 Rename-Computer parameters
$Old ="OriginalName"
$New ="MyServer"
Rename-Computer -NewName $New -ComputerName $Old `
-Restart -WhatIf

Note 3: I added two variables $Old and $New; firstly I wanted to draw your attention to the need to adjust my script to your circumstances, secondly, I wanted to plant the idea that you could modify this script to rename a bunch of computers.

Note 4: The backtick ` means wrap to the next line.

Note 5: Remove the -WhatIf when you are ready to actually rename the computer.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Research Additional Parameters for Rename-Computer

To let you into a secret, after researching this cmdlet I discovered that I could replace two lines of code in my draft script with a single parameter called -restart.

# Research PowerShell Rename-Computer parameters
Get-Help Rename-Computer -full

Note 6: I always like to append -full to this command so that help gives me examples.  Incidentally, ‘Get’ is optional because PowerShell assumes that ‘Get-‘ is the intended verb if you just use a noun such as ‘Help’.

Result: Thanks to Get-Help I discovered other useful parameters such as, -Force, -PassThru and LocalCredential.

PowerShell Script to Rename Multiple Computers

I am going to build this script in stages.  Once each stage works, we can glue them together and make a useful script that will rename a list of computers.  Whether or not you need to extend Rename-Computer to multiple machines, it’s worth examining techniques such as assigning variables, looping, and error control.

Stage 1: Create a Foreach loop to read names from a .csv file.PowerShell 3 Rename-Computer Name

Assumption: You have a .csv file with the names of your computers.  There is no need for column headers in the file because PowerShell takes care of the field names with -Header.

$CSVFile ="D:\Experiment\RenameComp.csv"
$List = Import-Csv $CSVFile -Header OldName, NewName
Foreach ($Machine in $List) {
Write-host $Machine.Oldname, $Machine.NewName
}

Note 7: Write-Host is merely to check that you can read the .csv file; please change the path to reflect your filename.

Stage 2: Let us change the payload to actually rename the computers.

# Rename-Computer Name with PowerShell
Clear-Host
$CSVFile ="D:\Experiment\RenameComp.csv"
$List = Import-Csv $CSVFile -Header OldName, NewName
Foreach ($Machine in $List) {
Rename-Computer -NewName $Machine.NewName `
-Computer $Machine.OldName -Force -Restart
}

Note 8: You can control what happens should a listed machine be offline by appending the -ErrorAction parameter.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Researching PowerShell Cmdlets with Simlar Noun

# PowerShell Item Cmdlet Research
Clear-Host
Get-Command -Noun Rename

I was surprised to find any other cmdlets that renamed object, but this is the result of looking:

Name
—-
Rename-NetAdapter
Rename-Computer
Rename-Item
Rename-ItemProperty
Rename-WebConfigurationLocation

See how to Change the Computer’s Description »

Summary Windows PowerShell Rename-Computer

The simple scripts on this page show how easy it is to rename one computer, or a whole batch if you prefer.  I hope that along the journey you learned useful parameters for Rename-Computer, and furthermore, gained experience of looping techniques to read a list of computer from a file.

If you like this page then please share it with your friends

 


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • Get-Counter  • IpConfig   • PowerShell v3 Ipconfig

Monitor Performance – PowerShell   • PowerShell Create Shortcut   • PowerShell Function Shortcut

PowerShell Restore Computer  • PowerShell Temp  • PowerShell Get-Item Env:  • PowerShell NetSh

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.