Refresh environment variable

12

2

How can I refresh a Windows cmd shell after I modified my Environment variables?

i.e. in bash I can just do "source ~/.bashrc"

Don Ch

Posted 2010-04-12T09:20:02.390

Reputation: 4 869

Close the console and open a new one. – Synetech – 2012-02-20T01:38:05.327

There's a good answer to the same question on Stackoverflow.

– Hugh Allen – 2010-04-12T10:40:22.723

Answers

4

It really depends on how your environment variables were set. If, for example, you used the "SET" command in the command line, it only effects the current instance. On the other hand, commands such as "SETX" will permanently adjust the value of the environment variable. SETX will ensure that all future instances of cmd will see the update. It will not effect the local environment.

If the environment variable is set by another application and is not being updated, it is often easiest to either restart the "explorer.exe" process, log out and back in, or reboot. Essentially, you just need to tell explorer to reload the environment variables. If it is an application that you have developed, such as an installer, ensure that it is calling SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment");. This tells explorer to refresh its cache of the environment variables.

This is important because applications launched by another process inherit their environment variables from their parent. As such, if you launch cmd from explorer, you get the variables as explorer sees them.

Chris Crutchfield

Posted 2010-04-12T09:20:02.390

Reputation: 73

1

Setting and retreving the master environment in Windows is something that the BartPE people spent a good deal of time on.

The main environment is stored in registry, but the explorer shell reads this to its address. From here it is sent to each cmd.exe session.

SET affects the cmd environment only. SETX allows you to change the master environment, but the setting is not reflected in the local environment.

Frank Westlake's CONSET and 4NT can fetch entries from registry, but no switches for the master environment in particular.

Normally loaded windows keeps track of settings to registry, while the one loaded through minint (ie WinPE) does not. So setting registry and reading registry works in both, but only normally booted windows will see the settings and pass them onto a new cmd,exe program.

wendy.krieger

Posted 2010-04-12T09:20:02.390

Reputation: 660

0

that depends where you "set" your environment variables. the equivalent to your bash example would be like this:

file: env.bat

set FOO=bar

and now you refresh it

% call env.bat

but i think you have done it via the windows-system-settings-dialog (or whatever its name is). the result of this is that you have actually changed the registry. these registry settings are mapped into the process when you launch a new process.

so, you can either start a new process (cmd.exe in your case) OR you can read the registry keys reflecting the changes you want. you could also write a little .bat / .cmd script which gets the environment from the registry and changes the current enviroment accordlingy (if the mood strikes me i ll do this after lunch).

akira

Posted 2010-04-12T09:20:02.390

Reputation: 52 754

0

This is now significantly easier, no need to restart the shell:

  1. Open the start menu and search for RefreshEnv.bat
  2. Run RefreshEnv.bat

Observe that your newly added environment variables are available in any new Command/Powershell instance, verifying the refresh.

Lance McCarthy

Posted 2010-04-12T09:20:02.390

Reputation: 111

What do you mean by now? Since which version? Is that bat file available with windows? – Máté Juhász – 2020-02-24T19:38:49.967