Thursday, August 20, 2015

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


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?


Answer



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


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...