How do you re-source the environment variables in a command prompt in Windows?

by leeand00   Last Updated May 16, 2019 02:01 AM

In a bash shell, you can edit your environment variables, run source ./.bashrc and refresh the values of environment variables in an already open shell. Does Windows have a similar command that you can run to do this from the command prompt?

(for instance I might change my system %PATH% variable in the properties of Computer, and then want my existing cmd.exe window to refresh the environment variables from there.



Answers 3


SET variable=string

Variable is the new variable you want to create (or an existing one)

String is what you want to assign to variable

Example of storing a text string:

C:> SET _dept=Sales and Marketing

Source: http://ss64.com/nt/set.html

NOTE: Changes made with SET will only remain for the CMD session.

Bitten Fleax
Bitten Fleax
June 28, 2016 14:05 PM

In a DOS window, you can send all of your environment variables into a batch file and run that to change the variables for the duration of that session:

C:\temp> set >newenv.bat

Click back into windows and pull the newenv.bat file up in your favorite editor and delete all of the variables except the ones you want to reset. For the ones left, add the word 'set' at the beginning of the line and rewrite everything after the '=' with your new values.

Now go back into the DOS window and run the batch file:

C:\temp> newenv

...and you'll see each line resetting the variables to your new values. If you don't want to see the lines resetting put 'echo off' at the top of the .bat file.

This should work for everything including the PATH. You can keep this file handy when you need to get back that environment context at later times.

You can also add values to the beginning or end of current values by using the %VARIABLE% in your set command. For example, to add another search directory to path you can say: set PATH=%PATH%;c:\bin and it will add it to the end, leaving the current values intact.

There are lots of other gymnastics you can perform on environment variables with the set command including standard %VAR% variables supplied by cmd, etc... Try typing 'help set' from the DOS prompt for a lot of information on those. The basic idea, though is to stash changes into a bat file and run it to save yourself from retyping changes over and over...

P. Heffner
P. Heffner
June 28, 2016 14:50 PM

I don't know how to change an environment variable in the system GUI and then "learn" it in an existing command Window.

However, I don't use the GUI any more, because I can set both global and local environment variables at the command line. No GUI ever needed. If you have a way to always avoid using the GUI, perhaps your problem becomes more tractable?

The downside is that there is one command to set the global environment variable, and another for the local command window. I don't know a way to source them both in one command. Instead, I have a .BAT file that takes my variable name and value as arguments, and sets the global and local environment variable at the same time. The next command window or GUI app will see the new value. I don't think running GUI apps or other open command windows will however.

C:>SET_ENVVARS.BAT MYVAR MYVAL
SET %1=%2
CALL SETX %1 %2 /M

You have all the limitations of the command line, such as escape characters and quoting, but most of the time this works pretty well.

My .BAT file has a few bells and whistles in it, but at its core, that's it.

I very rarely have to have to use the GUI.

I also occasionally "dump" my environment variables with:

C:\>SET >MYVARS.TXT

A little smithing would let you source from that.

Its not a quick fix, but I invest a lot of time in my .BAT file infrastructure and find that in the long run I save more time by reframing my problem to match the way Windows .BAT files work best. Beyond that, sometimes I resort to C, for example to list all PATH elements, but each on a separate line.

James Bay
James Bay
January 11, 2017 12:10 PM

Related Questions


Updated April 02, 2018 17:01 PM

Updated October 28, 2017 16:01 PM

Updated December 16, 2016 08:00 AM

Updated August 29, 2017 14:01 PM

Updated June 08, 2017 23:01 PM