Change a computer name from the command-line

Purpose: Demonstrate different ways to change a computer name remotely using command-line

There are multiple reasons why you might need to change a computer name remotely. One might be that you are lazy. The other might be that you would disturbt the user if you change his workstation name with a RDP (or similar) connection. The third might be that you creating are a tool and want to use your command line as a function to it. What ever your case is, here are my foundings about this subject.

NETDOM
Command-line usage YES
Remote usage YES
Domain usage YES

My personal favorite is NETDOM. It is an application that enables administrators to manage Active Directory domains and trust relationships from the command prompt (TechNet Netdom). NETDOM is included in RSAT (Remote Server Administrative Tools) so it must be installed on the system it is called.

To use netdom from commandline

1
netdom renamecomputer COMPUTERNAME /newname:NEWCOMPUTERNAME /force
netdom renamecomputer COMPUTERNAME /newname:NEWCOMPUTERNAME /force

Netdom requires elevation, to use netdom with psexec (Sysinternals PsExec)

1
psexec.exe -h -e netdom renamecomputer COMPUTERNAME /newname:NEWCOMPUTERNAME /force
psexec.exe -h -e netdom renamecomputer COMPUTERNAME /newname:NEWCOMPUTERNAME /force

You need to restart the target workstation for the name to actually change.

Powershell
Command-line usage YES
Remote usage YES
Domain usage YES

You can use the modern approach of powershell (works in PS 1.0+). This is very similar to the WMI based method since you call the WMI query from powershell. This method will prompt the user for credentials to do the name change. However, the powershell-way is a also valid for domain computes.

1
2
3
4
(Get-WmiObject win32_computersystem).rename("COMPUTERNAME")
add-computer -Credential DOMAIN/USERNAME -DomainName DOMAIN
 
Restart-Computer
(Get-WmiObject win32_computersystem).rename("COMPUTERNAME")
add-computer -Credential DOMAIN/USERNAME -DomainName DOMAIN

Restart-Computer

You need to restart the target workstation for the name to actually change.

WMIC
Command-line usage YES
Remote usage NO
Domain usage YES

A valid way in Windows 7 workstations is to use the inbuilt WMIC. You will create a WMI-query to change the workstation name. However, you cannot change a remote domain computer with this tool. More information from the reference TechNet Rename method of the Win32_ComputerSystem class.

1
wmic /node:COMPUTERNAME computersystem where name=COMPUTERNAME call rename name=NEWCOMPUTERNAME
wmic /node:COMPUTERNAME computersystem where name=COMPUTERNAME call rename name=NEWCOMPUTERNAME

You need to restart the target workstation for the name to actually change.

REGISTRY / VBS
Command-line usage YES
Remote usage YES
Domain usage NO

A simple way to change a workstation name, HOWEVER, YOU CANNOT USE THIS METHOD ON DOMAIN COMPUTERS. You will lose domain trust the moment you do this (since nothing is changed to the Active Directory). Remotely registry change reference: Rename method of the Win32_ComputerSystem class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sNewName = "put new name here"
 
Set oShell = CreateObject ("WSCript.shell")
 
sCCS = "HKLM\SYSTEM\CurrentControlSet\"
sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\"
sCompNameRegPath = sCCS & "Control\ComputerName\"
 
With oShell
.RegDelete sTcpipParamsRegPath & "Hostname"
.RegDelete sTcpipParamsRegPath & "NV Hostname"
 
.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName
.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName
.RegWrite sTcpipParamsRegPath & "Hostname", sNewName
.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName
End With ' oShell
 
MsgBox "Computer name changed, please reboot your computer"
sNewName = "put new name here"

Set oShell = CreateObject ("WSCript.shell")

sCCS = "HKLM\SYSTEM\CurrentControlSet\"
sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\"
sCompNameRegPath = sCCS & "Control\ComputerName\"

With oShell
.RegDelete sTcpipParamsRegPath & "Hostname"
.RegDelete sTcpipParamsRegPath & "NV Hostname"

.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName
.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName
.RegWrite sTcpipParamsRegPath & "Hostname", sNewName
.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName
End With ' oShell

MsgBox "Computer name changed, please reboot your computer"

You need to restart the target workstation for the name to actually change.

One Thought on “Change a computer name from the command-line

  1. So basicly you could do a script that uses NETDOM and changes the computer’s name to let’s say… serial number perhaps? And then deploy this to multiple computers (that have a bad name) as a package with SCCM. Any experiences about this kind of usage of netdom?

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + eight =

Post Navigation