How to rename a computer with PowerShell
In this post, I want to show you how to rename a local or remote computer with the PowerShell cmdlet: Rename-Computer. To rename a computer with PowerShell, use the Rename-Computer cmdlet with the following syntax:
Rename-Computer `
-ComputerName <String> `
-NewName <String> `
-LocalCredential <PSCredential> `
-DomainCredential <PSCredential> `
-Restart
The workaround in a remote computer #
For computers that are in a workgroup, run the PowerShell console as administrator, and then type:
Rename-Computer `
-ComputerName "RemoteComputer" `
-NewName "NewName" `
-LocalCredential RemoteComputerAdminUser `
-Restart
For computers that are in a domain, run the PowerShell console as administrator, and then type:
Rename-Computer `
-ComputerName "RemoteComputer" `
-NewName "NewName" `
-DomainCredential DomainAdminUser `
-Restart
You can use a CSV file to rename multiple computers. #
The format used in the CSV file:
VMName01,VM-Name-01
VMName02,VM-Name-02
VMName03,VM-Name-03
VMName04,VM-Name-04
VMName05,VM-Name-05
Here is a simple script that allows you to rename several computers declared in a CSV file
# Rename-Computers-CSV.ps1
# v0.1
# wwww.jorgebernhardt.com
$File = 'c:\scriptComputerList.csv'
$Credential=Get-Credential
$computerList= Import-Csv `
-Path $File `
-Delimiter "," `
-Header OldName,NewName
foreach ($Computer in $computerList)
{
Rename-Computer `
-ComputerName $Computer.OldName `
-NewName $Computer.NewName `
-DomainCredential $Credential `
-Force `
-Restart
}
The workaround in a local computer #
For computers that are in a workgroup, run the PowerShell console as administrator, and then type:
Rename-Computer `
-NewName "ComputerName" `
-LocalCredential localhostAdminUser `
-Restart
For computers that are in a domain, run the PowerShell console as administrator, and then type:
Rename-Computer `
-NewName "ComputerName" `
-DomainCredential DomainAdminUser `
-Restart
It uses the Force parameter to suppress the confirmation prompt and the PassThru parameter to return the command results.
Thanks for reading my post. I hope you find it useful.
If you want to know more about the Rename-Computer cmdlet, check out this link.