How to change the name of a pc via batch file without having to go looking for the name beforehand
WMIC ComputerSystem where Name=COMPUTERNAME call Rename Name=NewName
This piece of script I found here tells you how to change the name, but first you have to look for it and what i need is for the batch file to find it store it to use it in that script to later change it.
If I have to go find it myself it's easier to just change via device properties since the batch file would contain other things and the script above would just get in the way. The script already contains setting UAC off, disabling firewall and creating a folder and sharing it (which I would also know how to set it to give full control to the permission everyone,
I saw it here but it didn't work.).(no it's nothing bad weird or wrong, it's just a bad company who does things the wrong way).
The easiest way is to use the %ComputerName%
system variable:
Wmic ComputerSystem where Caption='%ComputerName%' rename NewName
Or, using Wmic
to get the current name and use it in a loop to compose the command that sets the new name:
Command-line:
@for /f skip^=1^usebackq %i in (`Wmic /node:. ComputerSystem get Caption ^|findstr .^|More`)do @<con: Wmic ComputerSystem where Caption='%~i' rename NewName >nul &&@echo\zOk||@echo\nOp!
Batch file:
@echo off
for /f skip^=1^usebackq %%i in (`Wmic /node:. ComputerSystem get Caption ^| findstr . ^| More
`)do <con: Wmic ComputerSystem where Caption='%%~i' rename NewName>nul && echo\zOk || echo\nOp!
Obs.: Run as admin and restart to see the result.
This batch script can be run with admin rights and show you a menu with two options :
- [1] Rename Computer Name
- [2] Restore Old Computer Name
@echo off
Mode 85,10
setlocal enableDelayedExpansion
Title Rename PC with WMIC
If [%1] NEQ [Admin] Goto RunAsAdmin
Set "TmpFile=%AppData%\%~n0.txt"
Set "File_Old_PC_Name=%~dp0Old_PC_Name.BAK"
If Not Exist "%TmpFile%" (
echo "%ComputerName%">"%TmpFile%"
) Else (
echo "%ComputerName%">>"%TmpFile%"
)
Call :RemoveDuplicateEntry "%TmpFile%" "%File_Old_PC_Name%"
:menuLOOP
Mode 85,10
Title Rename PC with WMIC
Cls & color 0B
echo(
echo( ================================Menu================================
echo(
@for /f "tokens=2* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do (
echo %%A %%B
)
echo(
echo( ====================================================================
set choice=
echo( & set /p choice=Make a choice or hit ENTER to quit: || GOTO :EOF
echo( & call :menu_[%choice%]
GOTO:menuLOOP
::----------------------------------------------------------------------------------------------------------
:menu_[1] Rename Computer Name
cls & Mode 120,10
echo(
echo( Please Type a New Name for this PC :
set /P "NewName="
(
echo Answ = MsgBox("Are you sur to rename your computer as ""%NewName%"" ?"_
echo ,VbYesNo+VbQuestion,"%NewName% ?"^)
echo If Answ = VbYes then
echo wscript.Quit(0^)
echo Else
echo wscript.Quit(1^)
echo End If
)>"%tmp%\%~n0_.vbs"
Cscript /nologo "%tmp%\%~n0_.vbs"
If ["!errorlevel!"] EQU ["0"] (
Call :RenamePC "%NewName%" && Call :Ask4Reboot
) Else (
Goto :menu_[1]
)
Exit /B
::----------------------------------------------------------------------------------------------------------
:menu_[2] Restore Old Computer Name
cls
set /a Cnt=0
@for /f "delims=" %%a in ('Type "%File_Old_PC_Name%"') do (
set /a Cnt+=1
Set OLD!Cnt!=%%a
)
echo(
@for /l %%N in (1 1 %Cnt%) do echo %%N - !OLD%%N!
echo(
:get selection
set selection=
set /p "selection=Enter Old Computer Name number: "
echo you picked %selection% - !OLD%selection%!
Call :RenamePC !OLD%selection%! && Call :Ask4Reboot
Exit /B
::----------------------------------------------------------------------------------------------------------
:RenamePC
WMIC ComputerSystem where Name="%ComputerName%" call Rename Name="%~1"
Exit /B
::----------------------------------------------------------------------------------------------------------
:Ask4Reboot
(
echo Set Ws = CreateObject("wscript.shell"^)
echo Answ = MsgBox("Did you want to reboot your computer ?"_
echo ,VbYesNo+VbQuestion,"Did you want to reboot your computer ?"^)
echo If Answ = VbYes then
echo Return = Ws.Run("cmd /c shutdown -r -t 10 -c ""You need to reboot in 10 seconds."" -f",0,True^)
echo Else
echo wscript.Quit(1^)
echo End If
)>"%tmp%\%~n0.vbs"
Start "" "%tmp%\%~n0.vbs"
Exit /B
::-----------------------------------------------------------------------------------------------------------
:RunAsAdmin
cls & color 0E & Mode 90,5
echo(
echo( ==========================================================
echo( Please wait a while ... Running as Admin ....
echo( ==========================================================
Powershell start -verb runas '%0' Admin & Exit
::-----------------------------------------------------------------------------------------------------------
:RemoveDuplicateEntry <InputFile> <OutPutFile>
Powershell ^
$Contents=Get-Content '%1'; ^
$LowerContents=$Contents.ToUpper(^); ^
$LowerContents ^| select -unique ^| Out-File '%2'
Exit /b
::-----------------------------------------------------------------------------------------------------------