0

Lately I've started working on automating some of my tasks. Since I work with windows machines mostly I decided to learn batch and powershell.

For now I have faced issue with variables in CMD.

The task is extremely simple. I want this script to change the hostname of the computer. The new name should be user input. I want it to find default hostname (whatever it is) and then change it to what I tell it to.

At this moment I was using command:

hostname

set /p future_name="Input what you want Hostname to be: "
wmic computersystem where caption='%computername%' rename %future_name%

shutdown /r /t 5

The tutorials that I keep finding say that the hostname of PC should already be written in the code. However, this script is being used on multiple machines which all bear different names.

I understand that this is a newbie question but I got stuck here.

C:\Windows\system32>wmic computersystem where caption='%computername%' rename 'future_name'
Executing (\\DESKTOP-SMTHSMTH\ROOT\CIMV2:Win32_ComputerSystem.Name="DESKTOP-SMTHSMTH")->rename()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
        ReturnValue = 87;
};

Press any key to continue...

P.S.

I tried running the script with another variable which will be me just retyping the hostname displayed above but it doesn't work still.

hostname

set /p my_name_is="Please retype my current name provided above: "
set /p future_name="Input what you want Hostname to be: "
wmic computersystem where caption='%my_name_is%' rename %future_name%

:: shutdown /r /t 5
PAUSE

It also returns the same as before. Any help will be appreciated!

4
  • The ReturnValue reported in your above output suggests that you have, either specified an invalid character in your new name, or that new name exceeds the 63 characters limit. Commented Nov 23, 2020 at 12:09
  • @Compo Hi, thank you for taking your time to check out my question. Unfortunately it never even asks for my input in the first place. This very same syntax works in my different script but not here. Any ideas? Commented Nov 23, 2020 at 12:23
  • That worked perfectly. Thank you very much! I think I had syntax error as well since you have both %ComputerName% variable and %future_name% in brackets quotation marks while mine wasn't. Also, I think I was mistyping the default variable %ComputerName% as %computername%. One way or another, I received ReturnValue = 0 meaning that it was a success. Thanks a lot! How can I mark your answer as correct? Commented Nov 23, 2020 at 12:33
  • I have deleted my comment, and added it as an answer for you, to accept if necessary. Commented Nov 23, 2020 at 12:52

1 Answer 1

1

Using the Where clause methodology you've shown in your question code, as far as I'm aware, is flawed. You cannot use the Create verb with the Where clause.

You should therefore, given a valid name and length, use the Call verb with the Rename method instead.

@Set "FutureName=%COMPUTERNAME%"
@Set /P "FutureName=Input what you want your new name to be>"
@"%__AppDir__%wbem\WMIC.exe" ComputerSystem Where "Name='%COMPUTERNAME%'" Call Rename "%FutureName%"
@"%__AppDir__%wbem\WMIC.exe" OS Call Reboot
Sign up to request clarification or add additional context in comments.

1 Comment

There should be no reason why %ComputerName%, %computername%, %COMPUTERNAME%, or even %cOmPuTeRnAmE%, should make a difference to your issue, @GArts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.