Nov 8, 2023

Batch : Rename Hostname via Batch File

This is an example of a batch file to rename the Hostname or Computer Name as per your preferences, it is much faster way to do computer renaming for many computers.

Note that the batch file must be "run as administrator" mode in order to works.

 @ECHO OFF
CLS
>NUL CHCP 65001

:asktorename
REM To ask whether to Rename the Hostname or not.
ECHO.
ECHO.
ECHO.
ECHO               ╔═════════════════════════════════════════╗
ECHO               ║         RENAMING THE HOSTNAME           ║
ECHO               ╚═════════════════════════════════════════╝
ECHO.
CHOICE /M "──────────────► DO YOU WANT TO RENAME THE HOSTNAME "
    IF ERRORLEVEL 2 GOTO eof
    IF ERRORLEVEL 1 GOTO askforname
GOTO eof
REM --------------------------------------------------------------------------------------

:askforname
REM To ask User for the preferred hostname.
ECHO.
ECHO.
SET /P NEWNAME="PLEASE TYPE THE NEW HOSTNAME: "
ECHO.
ECHO "     ► ► ► THE NEW HOSTNAME IS ───► %NEWNAME%"
CHOICE /M "     ► ► ► IS THIS CORRECT "
    IF ERRORLEVEL 2 GOTO asktorename
    IF ERRORLEVEL 1 GOTO dorenhost
GOTO eof
REM --------------------------------------------------------------------------------------

:dorenhost
REM To proceed Rename the Hostname as per Input by User.
ECHO.
ECHO "     ► ► ► OK, RENAMING HOSTNAME → %NEWNAME%, PLEASE WAIT..."
WMIC COMPUTERSYSTEM where name="%COMPUTERNAME%" CALL RENAME name="%NEWNAME%"
TIMEOUT /t 3 /NOBREAK
ECHO.
ECHO "     ► ► ► RENAMING HOSTNAME COMPLETE"
ECHO.
ECHO "THE NEW HOSTNAME WILL TAKE EFFECT AFTER COMPUTER RESTART"
TIMEOUT /t 5 /NOBREAK
GOTO eof
REM --------------------------------------------------------------------------------------

:eof
START SHUTDOWN /r /f /t 10
COLOR
>NUL CHCP 437
EXIT /b



!!! HAPPY COMPUTING !!!

No comments:

Post a Comment