user1256825
user1256825

Reputation:

Change the hostname using the batch script

Hi i wanted to change the value of hostname for new computer but things didn't work as i try to input the value and getting the value for the hostname. I try to restart the computer the result is still the same.. I am testing to see whether this script is capable of updating the hostname for pcs or computer name

thanks

here is my code

REM This script runs in MS-DOS Batch File Language 

@echo off
set /p id= Enter ID or Hostname: 
echo %id%

WMIC computersystem where caption='%ComputerName%' rename %id%


REM exit the applications
echo "Export completed successfully. Press any key to exit"
pause >nul
exit /B

Upvotes: 0

Views: 8536

Answers (2)

Rohan
Rohan

Reputation: 1

I tested the below batch script to change the windows 10 hostname through batch script and it work perfectly fine try it out and reboot the system atlast !

set /p NEW_NAME="Please enter computer name: "
for /f %%i in ('hostname') do set OLD_NAME=%%i
echo %OLD_NAME%
echo %NEW_NAME%
WMIC computersystem where caption="%OLD_NAME%" rename "%NEW_NAME%"
pause

Upvotes: 0

Compo
Compo

Reputation: 38579

Here's how I'd probably do it:

@Echo Off
Echo Your current name is %ComputerName%

:AskID
Set "ID="
Set /P "ID=Enter your new name: "
If Not Defined ID (Echo Can not be empty
    GoTo AskID)
If /I "%ID%"=="%ComputerName%" Exit /B
If "%ID:~,1%"=="." (Echo Must not begin with a period
    GoTo AskID)

Rem Put here some more checks for disallowed words or characters


WMIC ComputerSystem Where Name="%ComputerName%" Call Rename "%ID%"

Notes

  • This will need to be run 'As administrator'.
  • The change will not take effect until the next reboot.
  • It is important that you don't allow your end user, especially when running an administrative task to just enter anything at the prompt. Please consider following the Remarked line's advice.

Upvotes: 2

Related Questions