Why PATH environment variable value is different if we open command prompt from different locations?

0

I have a package which adds and removes an entry (say C:\my_entry) to PATH environment variable during installation and uninstallation respectively. I am able to see that entry is being added/removed to/from PATH variable by verifying the registry key HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path.

But I observed different values for PATH environment variable from command prompt.

After installation

If I open command prompt as Win+R, Type cmd and then enter, I am able to see C:\my_entry in PATH variable.

If I open command prompt(cmd.exe) from C:\Windows\System32 directory, I am unable to C:\my_entry in PATH variable.

After un-installation

If I open command prompt as Win+R, Type cmd and then enter, I am still able to see C:\my_entry in PATH variable though it got removed from registry.

Please let me know why PATH environment variable value is different if we open command prompt from different locations? And why the PATH environment variable value is different in command prompt from the value present in Registry?

gopu

Posted 2016-08-24T10:58:30.180

Reputation: 3

Answers

0

Environment variables are usually inherited – a new process gets a copy from whatever launched it, not from the registry. (ProcExp or Process Hacker can show the env of each individual process, along with the actual hierarchy.)

The only times the registry keys are read are when you log in, and when some program (such as an installer) tells all apps to refresh their env. (Usually only Explorer reacts to that request.)

So it could be that one Explorer process (the one handling the taskbar shell) refreshed its environment after installation, but the one handling folder windows didn't. (Or maybe you ran cmd from a third-party file manager.)

user1686

Posted 2016-08-24T10:58:30.180

Reputation: 283 655