Skip to content

Instantly share code, notes, and snippets.

@timnew
Last active March 22, 2025 07:46
Show Gist options
  • Save timnew/2373475 to your computer and use it in GitHub Desktop.
Save timnew/2373475 to your computer and use it in GitHub Desktop.
Script to Rename Computer without Reboot
$ComputerName = "New Name"
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname"
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname"
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\Computername" -name "Computername" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\ActiveComputername" -name "Computername" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname" -value $ComputerName
Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "AltDefaultDomainName" -value $ComputerName
Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "DefaultDomainName" -value $ComputerName
@sebma
Copy link

sebma commented Nov 25, 2020

@timnew Congratulations for this great script.

Can you please add some restrictions on the new name before actually applying the new name to windows, that is to say : forbid names containing . or spaces just like the warning printed by the netdom.exe tool :

The syntax of the new computer name, WORD1.WORD2
is incorrect. A computer name (DNS host label) may contain letters (a-z, A-Z),
numbers (0-9), and hyphens, but no spaces or periods (.).

The filename, directory name, or volume label syntax is incorrect.

The command failed to complete successfully.

@sebma
Copy link

sebma commented Nov 25, 2020

@timnew The reboot might not be needed, but my question is : Do I need to kill all explorer.exe instances and relaunch one for the new registry key to be reloaded ?

@PharQ
Copy link

PharQ commented Mar 5, 2021

i'm trying to rename my PC to its IP address this is what I have but i get an error saying the syntax of the command is incorrect

@echo off
SET /P < ipaddress >
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %PCNAME% /f

@overdrive80
Copy link

overdrive80 commented Feb 5, 2023

@echo off

set "cName=ROG-ISRA"
set "OEM=ROG STRIX B650E-F GAMING WIFI"

REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation" /t REG_SZ /v Model /d "%OEM%" /f

wmic computersystem where name="%computername%" call rename name="%cName%"

REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "Hostname" /d %cName% /f
REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /d  %cName% /f

set "sysSets=SystemSettings.exe"

tasklist /fi "ImageName eq %sysSets%" /fo csv 2>NUL | find /I "%sysSets%">NUL
if "%ERRORLEVEL%"=="0" (
	taskkill /F /IM %sysSets% 
	start ms-settings:
)

EDIT: More simply and best

@Raazor
Copy link

Raazor commented Aug 6, 2024

FYI I had the issue that I needed a name change without reboot AND running PsExec for another user session on the same machine afterwards. I got this Error: Creating key file on $newComputername. The user name or password is incorrect / The network location cannot be reached.

Fix was to add the following lines to rename.ps1

# Restart service to have PsExec working after name change; wait 5 seconds to be safe
Restart-Service Server -Force
Start-Sleep 

@Hdwrguy
Copy link

Hdwrguy commented Dec 10, 2024

So I found this script and thought it would be awesome. The script does rename the computer and it looks good from that end, but in AD, the name does not update. This causes issue with SCCM and other management tools. I ended up renaming them the old fashioned way but with Powershell that prompts me for my AD PW.

@ricoroodenburg
Copy link

So I found this script and thought it would be awesome. The script does rename the computer and it looks good from that end, but in AD, the name does not update. This causes issue with SCCM and other management tools. I ended up renaming them the old fashioned way but with Powershell that prompts me for my AD PW.

That makes sense; you can't just change a name in Active Directory without authentication. That system is completely separate from the local machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment