How to set the value of an environment variable to the output of a command in a WIndows batch file?

Solution 1:

Temporarily:

for /f "delims=" %a in ('command to run') do @set example_environment_variable=%a


Permanently:

for /f "delims=" %a in ('command to run') do @setx example_environment_variable=%a

Sidenote, set sets it just for this command process/window, but setx sets it for the whole user/system.

Solution 2:

Try running these commands:

for /f "delims=" %a in ('mybatchfile.bat') do @set myenvvar=%a
echo %myenvvar%