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

0

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.

leeand00

Posted 2016-06-28T13:48:59.593

Reputation: 14 882

Related, possible dupe: http://superuser.com/questions/130029/refresh-environment-variable?rq=1

– Mokubai – 2016-06-28T13:53:42.897

You can't. The easiest solution is to close and open a new cmd shell. – DavidPostill – 2016-06-28T13:54:04.340

1That sucks. Do you hear me Microsoft? That sucks. – leeand00 – 2016-06-28T13:54:45.080

Answers

0

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

Posted 2016-06-28T13:48:59.593

Reputation: 632

Please read the question again carefully. Your answer does not answer the original question. – DavidPostill – 2016-06-28T15:06:59.673

Yes but he can just set it in there so no need to refresh?? – Bitten Fleax – 2016-06-28T15:38:35.197

"I might change my system %PATH% variable in the properties of Computer" ... – DavidPostill – 2016-06-28T17:33:25.210

0

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

Posted 2016-06-28T13:48:59.593

Reputation: 184

Please read the question again carefully. Your answer does not answer the original question. – DavidPostill – 2016-06-28T15:07:29.597

0

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

Posted 2016-06-28T13:48:59.593

Reputation: 1