Skip to content

Instantly share code, notes, and snippets.

@timnew
Last active Sep 22, 2021
Embed
What would you like to do?
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
@artemyarulin

This comment has been minimized.

Copy link

@artemyarulin artemyarulin commented Aug 10, 2014

Thank you very much for the script!

@cotarelo2

This comment has been minimized.

Copy link

@cotarelo2 cotarelo2 commented Jul 28, 2017

Thank you for the script! You could add

-join ((65..90) + (97..122) | Get-Random -Count 5 | % {[char]$_})

So the computer name is generated randomly

It would be awesome if one could access the virtual machine name from vmware

@lollenka

This comment has been minimized.

Copy link

@lollenka lollenka commented Aug 4, 2017

I'm not too good at it. Could you please tell me, after I replace the computer names here with the ones I need, what do I do with this script? Do I save it under some specific name and paste into system files? I have Win10. Please.

@TheBoroer

This comment has been minimized.

Copy link

@TheBoroer TheBoroer commented Sep 18, 2017

@lollenka This is a powershell script. You can just open a powershell window and then paste this in

@audioscavenger

This comment has been minimized.

Copy link

@audioscavenger audioscavenger commented Apr 17, 2018

you need to be admin AND enable your Your Execution Policy as described here

@discentem

This comment has been minimized.

Copy link

@discentem discentem commented Apr 23, 2018

This seems to cause Not enough storage is available to complete this operation for just about every command I try to run after editing the registry in this way on Windows Server 2008.

@thangvc91

This comment has been minimized.

Copy link

@thangvc91 thangvc91 commented Aug 2, 2018

Hi Cotarelo ,
I would like to ask you about you comment on July 27 2017 , could you please help a full code after adding the "-join ((65..90) + (97..122) | Get-Random -Count 5 | % {[char]$_})".I don't know where i can put the code. Sorry for my bad question ?

@uluQulu

This comment has been minimized.

Copy link

@uluQulu uluQulu commented Dec 21, 2018

Thanks @timnew, worked flawlessly on Windows 7 Home Basic.

I just also had to run,
Set-ExecutionPolicy RemoteSigned
at C:\Windows\SysWOW64\WindowsPowerShell\v1.0\ powershell.exe as Administrator (thanks to Glen Little).

@fragtion

This comment has been minimized.

Copy link

@fragtion fragtion commented Jul 30, 2019

Worked great. Thank you

@linotes

This comment has been minimized.

Copy link

@linotes linotes commented Oct 30, 2019

Thanks, man. It really helps out a lot.

@rererecursive

This comment has been minimized.

Copy link

@rererecursive rererecursive commented Jul 14, 2020

This breaks Powershell/other applications if the hostname is too long. For example, run it with $ComputerName = "hello.there.my.name.is.john" and then open another Powershell window; you'll find a cryptic error message. My guess is something is breaks in the registry.

@sebma

This comment has been minimized.

Copy link

@sebma 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

This comment has been minimized.

Copy link

@sebma 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

This comment has been minimized.

Copy link

@PharQ 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

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