230

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?

4
  • 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 8, 2009 at 20:39
  • 4
    You have to close the command prompt, and reopen it again, for your path variables to update. The variables are loaded when cmd starts. Oct 20, 2014 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. Mar 3, 2020 at 9:21
  • Open the Task Manager, find there the Explorer process, restart it using the corresponding context menu item. Mar 15, 2020 at 20:29

7 Answers 7

195

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

12
  • 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 %<myvar>%. Thank you both for your answers. May 21, 2009 at 1:09
  • 5
    For PowerShell users this snippet may be of help: poshcode.org/2049 Oct 12, 2011 at 6:32
  • 9
    If using cmd, you need to restart it if change env variable Dec 13, 2012 at 5: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 Jan 19, 2015 at 10:12
  • 5
    404 for the link :-(
    – Samoth
    Apr 12, 2019 at 9:58
92
  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.

15
  • 6
    Out of all the answers on this page, this is the only one that actually worked for me. Thank you!! Dec 6, 2013 at 7: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, 2014 at 15:48
  • 9
    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. Aug 21, 2015 at 20:14
  • 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. Nov 29, 2018 at 15:25
  • 1
    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, 2021 at 9:46
22

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

2
  • 4
    This problems affects ALL SERVICES, even a restarted service will not see new environment variables.
    – gimel
    May 16, 2009 at 10:39
  • 1
    Are you sure it's not because of sharing a svchost process?
    – Mark Sowul
    Nov 26, 2013 at 22:57
19

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.

1
  • 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. Jan 15, 2019 at 19:33
7

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.

3
  • doesn't work :/ "invalid syntax"
    – Joe DF
    Mar 28, 2019 at 17:58
  • 1
    Try now. Apparently it needed double-quotes.
    – Doug
    Mar 28, 2019 at 19:53
  • Thanks, yeah I figured.... except I get a truncation warning, so I can't add to it... :(
    – Joe DF
    Mar 28, 2019 at 20:14
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

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.

You must log in to answer this question.

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