6

I just spent the last hour on trying to find out why the hell my PATH variable wasn't updating for my cmd.exe. Now I figured out that it kind of did update, but only for certain conditions...
I updated it via Win+Break -> Change Settings -> Advanced -> Environment Variables...

Now when I open a new commandline via Win+R -> cmd -> Enter the PATH variable shows whatever I set it to.
But when I open a new commandline via Shift+Rightclick into folder -> Open command window here, the PATH variable shows outdated content.

enter image description here

My question: Why is this happening, what can I do about it?

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • 5
    You updated the registry with the 1st step. Those changes don't become effective until a process is started and it doesn't inherit the environment of its parent process. You'd have to restart Explorer.exe, easiest way to do so is to logout and login again. – Hans Passant Apr 26 '15 at 14:08
  • @HansPassant Wow... that solved it. Thank you, post it as an answer so I can accept it. – Forivin Apr 26 '15 at 14:16
  • 1
    @HandPassant, top-level windows receive a `WM_SETTINGCHANGE` message when the control panel updates the environment settings, for which Explorer calls `shell32!RegenerateUserEnvironment` to reload its environment. So how is that Explorer (the parent in both cases) is using the new environment in one case but not the other? Maybe later I'll look at this in the debugger to see what's going on. Restarting the session solves the problem, in a superficial sense, but it doesn't explain the behavior. – Eryk Sun Apr 26 '15 at 18:19
  • @eryksun: I have a feeling that "open command window here" is a third-party plugin. (I don't see it on my machine.) So probably it just doesn't know where to find the "up to date" version of the environment block. – Harry Johnston Apr 27 '15 at 06:06
  • @HarryJohnston, it's in Windows 7. It shouldn't open an elevated command prompt, but probably Forivin just has UAC disabled. – Eryk Sun Apr 27 '15 at 12:34
  • Yes, UAC is completely disabled and I'm on Windows 8.1. The Shift+Rightclick thing is implemented since Windows 7 afaik. – Forivin Apr 28 '15 at 14:47
  • @HansPassant I just noticed this question was solved in the comments. It would probably a good idea to make that comment an actual answer. – Forivin Oct 11 '15 at 00:17
  • Sure, don't hesitate to post the answer yourself and accept it. – Hans Passant Oct 11 '15 at 00:50

1 Answers1

6

As pointed out by Hans Passant, the problem was that running cmd via the explorer's context menu spawns cmd as a child process to the explorer causing it to inherit the environment variables from explorer.exe instead of acquiring them itself. And since a process usually only loads the environment variables once in the beginning and doesn't listen for changes, the explorer inherited outdated variables to the cmd instance.

So the solution would be to simply restart explorer.exe.

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • 1
    I'm fairly sure that Explorer is also the parent to programs run from the Start Menu, so I think the cause is a bit more complex than that - Explorer must be seeing the environment variable changes but for whatever reason the code that implements the context menu neglects to respect them. Doesn't really matter though, since the resolution is the same either way. – Harry Johnston Oct 11 '15 at 22:26
  • They run as children to different explorer processes. http://i.snag.gy/wYivz.jpg But you're probably right and it is a bit more complicated than I thought. Maybe it just happens to all processes that are a children of services.exe or so... – Forivin Oct 12 '15 at 13:52
  • Wow, that's really weird. I mean, given that it's a separate instance of `explorer.exe` running in a service context it makes perfect sense that the environment variables aren't up to date. I'm just baffled as to *why* there's a separate instance running in a service context! Incidentally, that could have any number of other side-effects on programs run from that command window, some of which are probably worse than the environment variable issue. So I'd recommend avoiding that particular menu option. :-) – Harry Johnston Oct 12 '15 at 20:33
  • 1
    The "separate instance running in a service context" arises when you have the "Launch folder windows in a separate process" Option set in Explorer or choose File > Open new window > Open new window in new process. The spawned process is an out-of-process COM server which apparently is unaware of (or insensitive to) the WM_SETTINGCHANGE message. – EricLaw Oct 11 '18 at 12:44