-
-
Save nycmitch25/01a30409b99f717df6f0f28f2d21ea37 to your computer and use it in GitHub Desktop.
Script to Rename Computer without Reboot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Set Computer Name (HOSTNAME) and Description without rebooting. Author M Quick Date.24/06/2021 Ver. 20210624 | |
.DESCRIPTION | |
Script sets environment variables and other needed values to change the hostname on a Windows 10 Workstation. The | |
script also sets the 'description' of the system. The result of the function calls return boolean results. The script | |
prints out HOSTNAME-DESCRIPTION-CONFIG-SUCCESSFUL if both values were set correctly. This is done so the text can be | |
captures by the AutoIT script running it and determine pass or fail (easier than getting the exit value). | |
.EXAMPLE | |
Rename.ps1 -CN NEWHOSTNAME -DS 'The host description goes here' | |
.INPUTS | |
-DN HOSTNAME and -DS DESCRIPTION | |
.OUTPUTS | |
Result :HOSTNAME-DESCRIPTION-CONFIG-FAILED | |
Detail: (failure on Changing ComputerName) | |
-ex. success- | |
Result :HOSTNAME-DESCRIPTION-CONFIG-SUCCESSFUL | |
Detail: (finished Success!) | |
#> | |
Param ( | |
$CN = $arg[0], | |
$DS = $arg[1] | |
) | |
function RenameComputer | |
{ | |
param($ComputerName) | |
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname" | |
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname" | |
New-PSDrive -name HKU -PSProvider "Registry" -Root "HKEY_USERS" | |
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 | |
Set-ItemProperty -path "HKCU:\Volatile Environment" -name "LOGONSERVER" -value $ComputerName | |
[Environment]::SetEnvironmentVariable("COMPUTERNAME", "$ComputerName", "User") | |
$CurrentName = [System.Net.Dns]::GetHostName() | |
If ($CurrentName -NotMatch $ComputerName) { | |
Return $false | |
} | |
Return $true | |
} | |
Function SetDescription | |
{ | |
Param($adDescription) | |
Get-CimInstance -ClassName Win32_OperatingSystem | Set-CimInstance -Property @{ | |
'Description' = $adDescription; | |
} | |
$CurrentDescription = (Get-WmiObject -Class Win32_OperatingSystem).Description | |
If ($CurrentDescription -NotMatch $DS) { | |
Return $false | |
} | |
return $true | |
} | |
#$CN = $CN.trim() | |
$CN = ($CN -replace '\s', '') | |
$DS = $DS.trim() | |
$Status = "FAILED" | |
$Step = '(failure on Changing ComputerName)' | |
$Result = RenameComputer -ComputerName $CN | |
if ($Result -eq $true) { | |
$Step = '(failure on Changing Description)' | |
$Result = SetDescription -adDescription $DS | |
if ($Result -eq $true) { | |
$Status = "SUCCESSFUL" | |
$Step = '(finished Success!)' | |
} | |
} | |
Write-Host("Result :HOSTNAME-DESCRIPTION-CONFIG-"+$Status+"`n Detail: "+$Step) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment