20

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

i.e. In Bash I can just do source ~/.bashrc

2
  • There's a good answer to the same question on Stackoverflow.
    – Hugh Allen
    Apr 12, 2010 at 10:40
  • 1
    Close the console and open a new one.
    – Synetech
    Feb 20, 2012 at 1:38

8 Answers 8

7

I'd like to complete the Lance McCarthy’s answer.

The RefreshEnv.bat is a part of Chocolatey package manager, so if you use Chocolatey then you have RefreshEnv.bat installed. But you can also get this batch file directly from the Chocolatey GitHub (link to raw data). Save this file to your PATH and just run refreshenv from any your console to update environment.

1
  • 1
    Note that refreshenv will load the environment from the registry, but it will not tell explorer to do the same, so if it didn't, next process you run will have the old environment again.
    – Jan Hudec
    Jul 24, 2023 at 14:30
6

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.

0
2

I learned there is a much easier way to achieve this: There is a command called refreshenv.

Just run it and you will see the following:

enter image description here

E:>refreshenvewe w Refreshing environment variables eeefrom registry for cmd.exe. Please wait...Finished..

5
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 20, 2022 at 16:05
  • This worked perfectly and honestly I liked this answer a lot. It was concise. Maybe it could use some more supporting information but only tacked on to the end. I think sometimes we get these long answers that I have to read all of it to just see if there is a legitimate working solution. If its going to be a longer answer then maybe a TLDR is needed. I say this because I am typically reading through A LOT of answers at times and a lot of it does not apply to my situation. Ill keep reading and learning but its nice to see the solution up top first :) My opinion. Not fact. Mar 8, 2023 at 23:15
  • 1
    'refreshenv' is not recognized as an internal or external command. Windows Server 2016
    – cdalxndr
    Apr 27, 2023 at 9:40
  • You have to have chocolatey package manager installed. May 4, 2023 at 7:06
  • it is worth to mention that this only works for the current CLI environment session once it is exited you have to do it again untill the system is restarted Feb 12 at 14:39
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.

1

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.

[EDIT] The batch file comes with certain dev tools (i.e. Visual Studio), it's not a default on Windows. You can quickly get it be installing chocolatey, or even better you can copy the text save it locally https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd

3
  • What do you mean by now? Since which version? Is that bat file available with windows? Feb 24, 2020 at 19:38
  • I cannot find RefreshEnv.bat on any of my Windows 10 or Windows Server 2016 boxes. Instead, after installing Chocolatey, C:\ProgramData\chocolatey\bin\RefreshEnv.cmd is available. Feb 4, 2021 at 15:34
  • My apologies, I should have clarified this comes with most devtools. Chocolatey is probaly the fastest way to get it, or just download from the repo. I'll update my answer with the link. Feb 5, 2021 at 17:25
1

CMD does not allow to refresh the window to the changes made in the actual session. You have to chainload a new window running a script in any case, no matter the variant you chose.

Since you did not exclude PowerShell as an option, but since it is a given for all actually supported Operating Systems by MicroSoft allow me to add:

Alternatively you could (if you can/are allowed) use PowerShell, which gives you direct access to the actual sessions environment-variables. You may overwrite the specific variables content in $env with the changes you applied to the User/Machine-profile.

[System.Environment]::GetEnvironmentVariable("Path","Machine")

... contains the changes applied to the PATH-environment-variable for the machine-wide profile

[System.Environment]::GetEnvironmentVariable("Path","User")

... contains the changes applied to the PATH-environment-variable for the actual User-profile

So to make these changes immediately effective assign it to the according PATH-environment-variable of the actual session:

$env:Path=[System.Environment]::GetEnvironmentVariable("Path","User")

I found another question on StackOverflow that has some variants to offer for this approach:

PowerShell: Reload the path in powershell

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).

0
-1

In Win 11 (I haven't confirmed in prior versions of Windows), if you use Control Panel to update the value and save it (search the keyword path in control panel if you don't know were it is) then open a command prompt and execute the command that relies on the PATH and you'll find it has updated. If not, keep in mind command prompt windows which were open before the change was made will not be updated. Close any previously open Command Prompt windows and open a new one. You can of course confirm by typing PATH (in UPPER or lower case) and you should see the full PATH environment variable contents displayed.Image of Control Panel with keyword PATH entered in search bar enter image description here

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .