Search code examples
visual-studio-2008windows-7batch-fileenvironment-variables

Propagate Environment Variable via a Batch File?


I am setting some permanent environment variables via a batch file. Is it possible to propagate them without logging off via a batch file? I have seen this reference but would like to know if it is possible via a batch file as well.

Currently, when I set the environment variables (with Visual Studio closed) and fire up Visual Studio, the new environment variables are not recognized and a given project (which depends on the variables) fails to compile. If I log-off then log back on, they are. The same is the case if I want the batch file to detect whether the variable exists and if it does, do nothing.

Unless I log-off/on, when the batch file sets the environment variable, does its job and closes, if I re-run it, it will set the variable again (fails to detect that it already exists). If I log-off/on, then the batch file detects the variable (and informs the user) and Visual Studio compiles the given project without errors.


Solution

  • Not sure I can follow this. Environment variable values are only ever inherited from the process that starts a new process. When you set them in a .bat or .cmd file, you are altering only the variable values of the cmd.exe process that executes the batch file.

    If you then start devenv.exe from that same cmd.exe session then Visual Studio will have those altered/added environment variables as well.

    What does not work is starting Visual Studio from Explorer. That got started way back when you logged in. It got initialized with the system environment. Which you can modify with the Control Panel + System applet. Advanced, Environment variables button. To make the changes here effective, you have to restart a process so it gets initialized with these altered settings.

    Which in effect means that you have to restart Explorer. Yes, logging out does that.