@echo off
CLS
color 1f
TITLE Checking for Admin...
echo.
cd %systemroot%\system32
call :IsAdmin

:StartScript
REM Enable Unicode
chcp 65001 >nul

:Start
SET "NewCName="
SET "YN="
SET "strlength=0"
TITLE Change Computer Name
cls
color 1f
echo.
echo Certain characters are not allowed, spaces will be converted
echo to - {minus character}.
echo.
echo Pressing just enter without entering anything will close script.
echo.

echo ** Will attempt to convert to UPPERCASE and 16 Characters MAX or risk of computer boot loops. **
echo.
set /p "NewCName=Please enter your new computer/device name: "

IF DEFINED NewCName ECHO %NewCName%>"x" & FOR %%? IN (x) DO SET /A "strlength=%%~z? - 3" & del "x"

IF NOT DEFINED StrLength SET "StrLength=0"

IF "%StrLength%"=="0" GOTO :DoneScript

IF %strlength% GTR 16 echo. & echo Error: More than 16 characters entered... & timeout /t 2 >nul & GOTO :Start

IF NOT DEFINED NewCName GOTO :DoneScript

CALL :UpCase NewCName

REM Change any spaces to - (Minus) to prevent networking errors.
SET "NewCName=%NewCName: =-%"

REM Misc other Disallowed characters to remove/change.
REM There are limitations with batch scripting for certain special
REM characters which cannot be filtered.

SET "NewCName=%NewCName:(=%"
SET "NewCName=%NewCName:)=%"
SET "NewCName=%NewCName:\=%"
SET "NewCName=%NewCName:/=%"
SET NewCName=%NewCName:^*=-%
SET "NewCName=%NewCName:?=%"
SET "NewCName=%NewCName::=%"
SET NewCName=%NewCName:"=%
SET "NewCName=%NewCName:^^=%"
SET "NewCName=%NewCName:!=%"
SET "NewCName=%NewCName:@=%"
SET "NewCName=%NewCName:#=%"
SET "NewCName=%NewCName:^&=%"

:AskLoop
CLS
echo.
echo New Computer Name: %NewCName%
echo.
set /p "YN=Is this correct {Y/N and press Enter}: "
IF /I "%YN%"=="N" GOTO :StartScript
IF /I "%YN%"=="Y" GOTO :ActionRequest
GOTO :AskLoop

:ActionRequest
CLS
echo.
echo Modifying registry keys...
echo.
reg add "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v "ComputerName" /d "%NewCName%" /f >NUL 2>&1
reg add "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%NewCName%" /f >NUL 2>&1
reg add "HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" /v "Hostname" /d "%NewCName%" /f >NUL 2>&1 
reg add "HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" /v "NV Hostname" /d "%NewCName%" /f >NUL 2>&1

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "DefaultDomainName" /d "%NewCName%" /f >NUL 2>&1
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "AltDefaultDomainName" /d "%NewCName%" /f >NUL 2>&1

echo Finished updating the registry...
echo.
echo Now to update the DNS cache to match new name...

ipconfig /renew
ipconfig /flushdns
ipconfig /registerdns

echo.
echo Finished updating DNS Cache...

set "computername=%NewCName%"

echo.
echo Done.
echo.

Pause
exit

:DoneScript
echo.
echo Nothing entered - script ended.
echo.
Pause
exit
=====================================================================================
:IsAdmin
Reg query "HKU\S-1-5-19\Environment"
If Not %ERRORLEVEL% EQU 0 (
 Cls & echo. & Echo You must have administrator rights to continue ... & echo.
 Pause & Exit
)
Cls

exit /b

:UpCase
FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") DO CALL SET "%1=%%%1:%%~i%%"
exit /b
