2

I am trying to change the System paths on my computer.

For some reason when I open cmd via run my paths are correct as expected.

However when I Shift+Right Click in a directory + open cmd window here I get a old/different path that doesn't even show in the Path variable on neither USER or SYSTEM.

Example:

CMD from RUN:

C:\Users\PERSON>python
Python 2.7.7 (default, Jun  1 2014, 14:17:13) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

C:\Users\PERSON>pip
Usage:
pip <command> [options]

CMD from shift+right Click:

C:\Users\PERSON>python
'python' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\PERSON>pip
'pip' is not recognized as an internal or external command,
operable program or batch file.

I have changed the paths in both System and User to see if there was some odd issue there but the issue persists.

If you need any more info let me know.

Edit: I have restarted all command prompts in-between changes to the path.

Edit 2: Here are the paths

CMD from RUN:

C:\Users\PERSON>echo %path%

C:\ProgramData\Oracle\Java\javapath;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\EASE\Cygwin\Bin;
C:\bin;
C:\bin\Hardware;
C:\bin\OpenCV;
C:\bin\Qt;
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
C:\Program Files (x86)\Microsoft Visual Studio\VC98\Bin;
C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;
C:\Python27\;
C:\Python27\Scripts;
C:\Program Files (x86)\Skype\Phone\;
C:\Program Files\SlikSvn\bin;
C:\Program Files (x86)\GNU Tools ARM Embedded\4.7 2012q4\bin;

CMD from shift+right Click:

C:\Users\PERSON\Desktop>echo %path%
C:\ProgramData\Oracle\Java\javapath;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\bin;
C:\bin\Hardware;
C:\bin\OpenCV;
C:\bin\Qt;
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
C:\Program Files (x86)\Microsoft Visual Studio\VC98\Bin;
C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;
C:\Program Files (x86)\Skype\Phone\;
C:\Program Files (x86)\GNU ToolsARM Embedded\4.7 2012q4\bin;
Snhp9
  • 123
  • 3
  • even if you do set path=c:\abcdefg if you do `c:\windows\system32>calc` it should open calc Try that. And is python python.exe? try echo %PATHEXT% in both cmd prompts and compare. and try getting 'file' from cygwin or gnuwin32 and doing `file python.exe` for more info on it – barlop May 19 '15 at 00:48
  • What does `where python.exe` say? What's the value of `HKCR\Drive\shell\cmd\Command`, `HKCR\Directory\shell\cmd\Command` and `HKCR\Directory\Background\shell\cmd\Command` in the registry? – Karan May 19 '15 at 00:56
  • note that it's possible it isn't a path issue, and you haven't even demonstrated that the paths are different, by displaying the paths. And as i've shown, in the case of windows, regardless of path, you should be able to run a command in the current directory. – barlop May 19 '15 at 18:36
  • -1 you have left this question hanging and ignored the suggested things to test. It is quite possible that this has nothing to do with the PATH, in which case not only have you left the question hanging (thus lowering its value) but your title is then wrong too. You have not even tested to see if the PATHs are really different. – barlop May 20 '15 at 07:48
  • 1
    Ok I have added the paths. – Snhp9 May 20 '15 at 15:23

2 Answers2

2

After you change the path, you need to make sure to restart Explorer before attempting to open up all your terminals again. This way, the Explorer process takes in the new PATH and is able to transmit that new PATH to new programs that it executes.

oldmud0
  • 4,234
  • 3
  • 24
  • 43
  • This may be of interest as well: http://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w – paradroid May 19 '15 at 01:18
  • 1
    I've never had to restart Win Explorer to get a new command prompt window to register changes to the PATH. It's just not required. – Karan May 19 '15 at 05:59
  • I have gotten it to work another way. I will post about it in a second. I will try this method too just so others will know. – Snhp9 May 19 '15 at 16:26
  • Restarting didn't seem to work and when I post my answer you will most likely see why. Same with paradroid's answer. – Snhp9 May 19 '15 at 16:28
  • -1 saying that changing the path requires a restart of explorer is utter nonsense. Try opening a new cmd window – barlop May 19 '15 at 18:34
  • @barlop How is this nonsense? Windows does not magically propagate changes to PATH to every process, including Explorer. – oldmud0 May 19 '15 at 19:05
  • @oldmud0 Can you give an example where you have to restart explorer? When you change the path in the OS it gets set in the registry, and any new command windows will get the new path from there - without explorer restarting prior to starting new command windows. You give an example of terminals and you're wrong with that example. So, can you give an example where you do have to restart explorer? In your example restarting explorer makes absolutely no difference – barlop May 19 '15 at 20:07
  • Restarting the command window did not work in my case Restarting Explorer didn't work either. – Snhp9 May 19 '15 at 20:53
  • @Snhp9 yes indeed. There are some comments on your question that you could address – barlop May 19 '15 at 21:08
  • @barlop See [this answer](http://superuser.com/a/107558/278985). If Explorer isn't getting watching PATH changes then log off, then log back on. – oldmud0 May 19 '15 at 21:18
  • @oldmud0 You wrote "you need to make sure to restart Explorer " <-- that is wrong. The answer you link to was where the user had launched the cmd prompt in a very unusual way.. (and even he only needed to start an explorer process normally rather than from the program he was using, and even he didn't need to restart explorer) or he could change the non-default setting he was on. So even he didn't need to restart explorer, and he was a very special case. – barlop May 19 '15 at 23:20
  • To just say like a rule "After you change the path, you need to make sure to restart Explorer " <-- is just wrong. Not even true of his case or the case you link to. And the case you link to the guy would've had lots of explorer.exes (due to his non-default setting, that's what it does), and there'd be no need for him to close them all prior to starting a new one. And with the default setting he'd have had no problem. So either way the special case guy you link to for your example, wouldn't need to 'restart explorer'. – barlop May 19 '15 at 23:20
  • @barlop If you have any solution of your own, you are always welcome to post an answer. – oldmud0 May 20 '15 at 00:30
  • @oldmud0 i'm aware of that. But do you realize what you posted is wrong? – barlop May 20 '15 at 07:45
  • @barlop Please stop. The answer was not useful and you used your downvote well :) Now move on. – oldmud0 May 20 '15 at 11:38
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/23951/discussion-between-barlop-and-oldmud0). – barlop May 20 '15 at 15:48
1

When a process is started, by default the environment variables are copied from the parent process (who is making the request to create the new process) to the newly created process.

When you use Run method, the explorer.exe instance handling your desktop will create the cmd instance, but when you use the Shift+right click method, a different explorer instance, child of svchost.exe (child of services.exe, child of wininit.exe) will create the cmd instance.

If both explorer versions do not have the same environment blocks, the cmd instances will not have the same variables.

When the configuration of the environment variables is changed from system properties, the OS will send a WM_SETTINGCHANGE message to all top windows. The explorer instance handling the desktop will receive it and update its environment block, but the instance handling the file browsing (the one under svchost.exe) does not process the message (no, at this moment I don't know why) and its environment block is not updated (all this has been tested with sysinternal's ProcExp checking the processes environments).

How to solve? I don't know. Maybe (no, not tested, I don't have a compiler at hand now, just an idea) instead of using a HWND_BROADCAST to send the WM_SETTINGCHANGE message, a direct message to the file browser process could solve it (or not)

How to deal with it? Kill the explorer instance whose parent process is svchost.exe. When a new file browser is requested a new instance is started with the correct environment block.

For a rough approach, just to try, run from command line

wmic process where "name='explorer.exe' and CommandLine like '%/factory%'" call Terminate

to kill the file browser. When a new file browser is requested a new process will be created (now with the environment updated) and new cmd instances will see the changes.

Edited

After some tests with an api monitor, i have seen that the svchost.exe is creating the explorer process via a CreateProcessAsUserW api call. In this call, the lpEnvironment argument is not null (if it is null, the environment is copied from parent to child), so svchost.exe is creating the environment for the new process. But, what source is being used to create the new environment?

So, I directly change the variable in the registry (regedit) to ensure that no WM_SETTINGSCHANGE message is sent, killed the file browser explorer instance and create the two cmd instances. The result is

  • Run method: the cmd instance does not see the changes. As there was not any message to the parent, its environment was not changed and the new started process inherits the unchanged version from the explorer.exe that handles the desktop.

  • Shift+Click method: the changes in the registry are available.

So, svchost.exe is retrieving the information from the registry to create a environment block to pass to the newly created process.

MC ND
  • 1,501
  • 10
  • 13
  • The command worked perfectly Although both cmd's seem to inherit there paths from different places or the Run command gets more paths. But at least I can run this now whenever I need to update the paths. Would be nice for it to auto update without having to remember to run the command though. – Snhp9 May 20 '15 at 15:31
  • any idea where each command prompt is inheriting its path from such that they differ like that? – barlop May 20 '15 at 15:55
  • When I look in the registry the paths are from 2 different locations. It seems like Visual Studio 6 has all the paths the Shift + RightClick Cmd uses – Snhp9 May 20 '15 at 17:52
  • @Snhp9, answer updated. – MC ND May 21 '15 at 06:32