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

batch filescriptwindows

How can I set an environment variable to the output of a command in a Windows batch file? The command will return a single value of around 32 characters (e.g. type myfile.txt).

Best Answer

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.

Related Question