4

I want to execute one command and assign it's value to a variable within a batch file.

We know that running hostname command on a windows command prompt gives the PC name. I want to use hostname command and assign it's value to a variable within a batch file.

After googling about it, I've tried using below methods, none of these seem to work:

set CONTROLLER=hostname
set CONTROLLER=%hostname%
set CONTROLLER=%%hostname%%
set CONTROLLER=!hostname!

Kindly advise.

2 Answers 2

4

We can easily get the hostname/computer name through below command

set host=%COMPUTERNAME%
echo %host%
Sign up to request clarification or add additional context in comments.

Comments

1

Try Using

@echo off
for /f "delims=" %%a in ('hostname') do @set HOST=%%a
echo %HOST%
PAUSE

Where HOST is your variable and in place of 'hostname' you can use any other command you like as well.

2 Comments

I did came across this one, was looking for a less complex method to do this. See Rajesh's answer as well, I realize that'd work only for 1 particular command, but hey that's what I need right now. Thanks anyway

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.