216

I would like to add an Environment variable to a Windows machine (desktop or server) and be able to use it without rebooting that machine.

Say you have a production server which hosts a variety of apps and a new app needs a particular Environment variable to run. You do not want to reboot it while users are connected to your other apps. What choices do you have? I don't like the wait-until-a-good-time-to-reboot option. There must be a better way. What am I missing?

YakovL
  • 117
  • 6
Chad Braun-Duin
  • 2,179
  • 3
  • 14
  • 11
  • 2
    I had this same issue. I read somewhere that killing the explorer.exe process would update the variables and it worked. Then I just had to run explorer from the task manager. – user19694 Sep 08 '09 at 20:39
  • 3
    You have to close the command prompt, and reopen it again, for your path variables to update. The variables are loaded when cmd starts. – NiLInfinite Oct 20 '14 at 13:30
  • 1
    Opening a new `cmd` using the task manager or explorer window does not work (at least with Win10), but using the **start menu and typing `cmd`** and then checking the variable works. – Andreas Covidiot Mar 03 '20 at 09:21
  • Open the Task Manager, find there the Explorer process, restart it using the corresponding context menu item. – Ilya Lysenko Mar 15 '20 at 20:29

7 Answers7

188

Changes to environment variables should take effect immediately, if you make the change via the main Properties dialog for the computer in question (go to My Computer | Properties | Advanced | Environment Variables). After the changes are saved, Explorer broadcasts a WM_SETTINGCHANGE message to all windows to inform them of the change. Any programs spawned via Explorer after this should get the updated environment, although already-running programs will not, unless they handle the setting change message.

I'm not able to tell from your problem description what specific problem you're having with this. Can you tell us more about the specific scenario that isn't working?

This KB article may also be of use: How to propagate environment variables to the system

Sky Voyager
  • 252
  • 3
  • 7
Charlie
  • 2,956
  • 2
  • 20
  • 13
  • 3
    Ok, I guess I must have seen this behavior on a running service or something. I added a new environment variable using the method described above. Then, I was able to see the value after opening up a new command prompt and using the command "echo %%. Thank you both for your answers. – Chad Braun-Duin May 21 '09 at 01:09
  • 5
    For PowerShell users this snippet may be of help: http://poshcode.org/2049 – Anders Zommarin Oct 12 '11 at 06:32
  • 9
    If using cmd, you need to restart it if change env variable – Neil McGuigan Dec 13 '12 at 05:08
  • 4
    Just installed Scala on my PC but the PATH did not update; presumably the MSI did not broadcast the WM_SETTINGCHANGE message. I added a dummy SYSTEM variable i.e. CHANGE_TO_UPDATE=z, that I update to trigger the WM_SETTINGCHANGE message - problem solved – Darren Bishop Jan 19 '15 at 10:12
  • Thanks for the CHANGE_TO_UPDATE=z, that's exactly the solution I wanted. It worked for me and I didn't have to reboot. – iopq Mar 07 '15 at 20:14
  • Note that this is true for programs spawned by running programs as well. For example, opening a `git-bash` window from GitExtensions after updating the `PATH` environment variable, you would expect the `bash` window to have the new `PATH`, however you need to restart the _original_ program (GitExtensions in this example), as programs spawned by other programs will inherit the environment variables of the spawning program, not re-fetch them even though they're starting for the first time. – Leith Feb 24 '18 at 23:29
  • As the original poster pointed out the problem is when you are running as a service. Basically anyone who is a child of services.exe doesn't pick up the change until a reboot; I assume this is because services is a child of wininit and not explorer. – aolszowka Jan 30 '19 at 15:31
  • 5
    404 for the link :-( – Samoth Apr 12 '19 at 09:58
  • There is a powershell script to trigger this event on [powershellgallery.com](https://www.powershellgallery.com/packages/PSCI/1.0.4/Content/core%5Cutils%5CUpdate-EnvironmentVariables.ps1) – snydergd Oct 17 '19 at 22:32
  • I had to close and reopen cmd and it worked. Thanks! :) – Tadej Nov 12 '19 at 12:39
  • Sadly the mentioned KB article does not exist. I only found following: _A process inherits its environment from its parent, and the consequences of this simple statement_ https://devblogs.microsoft.com/oldnewthing/20150915-00/?p=91591 – escalator Apr 23 '20 at 15:24
  • What about IIS + ASP.NET. Does it notice? – A X Jan 02 '21 at 05:11
83
  1. In a command prompt type: runas /user:yourusername@yourdomain cmd
  2. It will open up a new cmd prompt, then type: taskkill /f /im explorer.exe
  3. Then type: explorer.exe

Now after closing all command prompts, you will see that the PATH variable has been truly updated.

All command prompts must be closed. Reopen a new command prompt, type path and you will see the new data.

Kara Marfia
  • 7,892
  • 5
  • 32
  • 56
  • 6
    Out of all the answers on this page, this is the only one that actually worked for me. Thank you!! – coredumperror Dec 06 '13 at 07:34
  • 2
    This worked for me. I think the problem is that if you run cmd through explorer (to save you having to type in long paths) then explorer is never closed, even if you close all explorer windows. Thanks for the solution :) – Steve Mc Sep 18 '14 at 15:48
  • Worked for me. You can also just use Task Manager to kill the explorer tasks and restart it (File > Run new task). – adabyron Jan 22 '15 at 16:48
  • 8
    Uhh. Please don't kill windows explorer unless one of its processes are hung. Instead, open the shutdown windows dialog and cancel it while holding `ctrl+alt+shift`. That will cleanly exit windows explorer. In Vista+ the shutdown windows dialog is harder to find (but still present, at least through 7 (unsure about 8 and 10)), so there is a second method. ctrl+shift+right-click in an empty part of the start menu and choose exit explorer. In 8 the exit option is the same, but you use the taskbar not the start menu. – Kevin Cathcart Aug 21 '15 at 20:14
  • The above comment describes the client OS's, but the same more or less applies to servers. I'm not sure if the shutdown with a reason dialog supports the exit explorer feature though, never had the opportunity to test it, and don't want to muck with any of the servers in my control. – Kevin Cathcart Aug 21 '15 at 20:16
  • Unfortunately, this did not work for me in Windows 10. It did not re-read the %PATH% variable from the registry, even for newly opened cmd windows. – mivk Apr 12 '17 at 14:26
  • @KevinCathcart and any others. It's 2.5 yr later and Kevin said he wasn't sure about Win10 then. I didn't know about the empty part of the start menu; double plus for that. And, I didn't get change to the environment variables until I restarted Win10. Down lower in this stream (authored by testworks) is an indication it would have been sufficient to log off and log on (rather than restarting). I didn't test that. I hope it helps someone. – BaldEagle Jan 01 '18 at 04:07
  • I think that this answer should definitely be the first one. – Nic Szerman Jul 22 '18 at 06:09
  • 1
    +1 This works as a charm on Windows 7. FYI, I simply used the CTRL+ALT+SHIFT and from the **Windows Task Manager I killed all explorer.exe process** and then started it again by hitting the **New Task** button. – Marco Demaio Nov 29 '18 at 15:25
  • The process doesn't get the WM_SETTINGCHANGE update until your restart it. – chrips Mar 10 '19 at 14:34
  • Approved on Windows 10. Thanks. But I don't know why I'm changing the system variable "path" and it is rolling back to previous value. There's some program or something changing it automatically. – Souza Oct 23 '19 at 11:39
  • It happens. I installed texlive and it overwrote my user PATH variable completely (I mean the user one, not the system one). I had to go back and edit it again. I use Rapid Environmental Editor and it is wonderful. – bgmCoder Feb 28 '20 at 20:09
  • Do applications inherit envars from applications that start them? This answer did not work for me at first... I'd restart explorer, close cmd, and open a new cmd, but the new envars didn't appear. But then, I ran Chocolatey's [RefreshEnv.cmd](https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd) right before restarting Explorer, and the cmd I started from _that_ Explorer had the new envars! But running RefreshEnv and opening a new cmd without restarting Explorer did not have the new envars. Strange. For context, I'm on Win 10. – Illya Moskvin Jun 26 '20 at 19:03
  • 2020 / 2021 update for restarting `explorer.exe` : Open up Task Manager (`Ctrl + Shift + Esc`). Find `Explorer.exe` in the list of active processes in the `Processes` tab. Right-click and select `Restart`. – rkeet Feb 15 '21 at 09:46
  • This worked for me, killing/restarting explorer from an elevated Powershell session. – Adam D. Mar 14 '22 at 10:41
20

One thing to keep in mind is that many programs obtain the environmental variables when they're first started, so while windows may not need a restart, some programs might before they'll be able to use the new variables. Good example of this is having to open a new command prompt window after adding a PATH (yes, I've been tripped up by this).

nedm
  • 5,610
  • 5
  • 30
  • 52
17

Whilst I don't have enough of a reputation to comment on the highest voted answer to this question, I would like to state that it is not exactly correct. I know this because no matter which workaround I tried in this post, nothing actually worked.

The kb article linked to in that answer actually states that:

However, note that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.

The part about the environment variables resetting to the previous values after reloading the command prompt is exactly what I experienced in Windows Server 2008.

The article goes on to say:

To effect these changes without having to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so that any interested applications (such as Windows Explorer, Program Manager, Task Manager, Control Panel, and so forth) can perform an update.

That does not imply that Explorer broadcasts a WM_SETTINGCHANGE message once you have changed the system environment variables, or that it actually works. I'm not sure how you would do what is suggested in the KB article (to propagate the changes immediately) from the command prompt.

testworks
  • 309
  • 2
  • 6
  • 1
    This is the only answer that's right for me. None of the others, including killing Explorer the hard way or the "proper" way, make any difference. – Hashim Aziz Jan 15 '19 at 19:33
5

Make the env. variable available straight away:

1. Open a shell

Depending on the environment variable you want to change do the following: (supose that you want to add a new PATH for a recently installed application) So, at the shell prompt, type the following:

2. PATH=%PATH%;C:\type\your\new\path\here

check that your new path has been added to the environment variable

3. echo %PATH%

Done.

Make the variable available on reboot

1. Press WinLogoKey+Pause/Break
2. On the left pane, press 'Advanced System Settings'
3. On the 'Advanced' tab, click 'Environment Variables'
4. In 'System Variables' choose the one you want to modify
5. Click Ok

Tested and working on Windows 7/10

Pedro Sousa
  • 194
  • 2
  • 5
5

Since people haven't added this answer yet, I wanted to mention that you can set permanent system variables that survive reboot with the SETX command.

e.g.

setx PATH "%PATH%;c:\temp\my\new\path"

Please note that the above will not work in the current command window and will require you to open a new command window before it takes effect. As a result, depending on the use case; you may need to combine this with one of the other techniques above.

Jeromy French
  • 309
  • 4
  • 15
Doug
  • 367
  • 5
  • 13
3

A possible solution for services is to run them temporarily as another user (other than LocalSystem, LocalService, NetworkService). For example for Apache service this works without any problems. To change service account open services.msc console, select service, click service properties and on second tab enter logon credentials for a user. Restart the service and it's environment variables should be up to date.

If this is a user which has been logged of than this should work without problems. If you are using current user account, then restarting explorer.exe might be necessary too. Also note that running services as normal user account might create security risks.

Odin
  • 151
  • 4