Why is my PATH environment variable different from the command prompt?

11

1

A bit of important background: my company has a generic login VBS script that makes modifications to the user PATH environment variable upon login and allows me to run software that has dependencies on mapped DFS fileshares. I also recently installed the Windows Powershell SDK to my Windows 7 Enterprise machine and attempted to try out modifying my PATH environment variable from the Powershell command line.

Following this, I noticed that I could no longer run applications that correspond to these login PATH modifications and that the environment variable editor PATH was set to something different than what was showing up when I issued an 'echo %PATH%' from the command prompt.

So for example (simplification), from the environment variable editor (My Computer properties -> Advanced System Settings -> Environment Variables) I had

 C:\MyDir\; C:\MyOtherDir

whereas when I did 'echo %PATH%' from a command prompt I got:

 C:\MyDir\

Has anyone else had a similar issue and was there some sort of resolution? When I googled for help, I came upon this:

(Related?) StackOverflow Thread

It occurred to me that if the login script was initiated by something other than my Explorer.exe environment, then that was the problem. However, when I ran the login script myself, my PATH from command prompt was unchanged. What would this have to do with PowerShell? I'm missing how this is connected to that install completely.

PatternMatching

Posted 2013-07-29T20:26:55.990

Reputation: 113

THE ANSWER BELOW IS GENIUS. – SDsolar – 2017-07-04T00:26:38.733

How did you modify the path in powershell? Where did you echo %PATH%? In cmd.exe? – Werner Henze – 2013-07-30T08:25:16.277

Answers

11

Windows has two classes of environment variables system environment variables and user environment variables. If you are using echo %PATH% you will see your user environment variable PATH. Only if there is no user variable defined, the system variable will be in effect for user processes.

The PATH separator ; should not be followed by a blank.

The following Microsoft note might be helpful:

You can modify user environment variables by editing the following Registry key:

   HKEY_CURRENT_USER \ 
         Environment

You can modify system environment variables by editing the following Registry key:

   HKEY_LOCAL_MACHINE \ 
               SYSTEM \ 
    CurrentControlSet \ 
              Control \ 
      Session Manager \ 
          Environment

Note that any environment variable that needs to be expanded (for example, when you use %SYSTEM%) must be stored in the registry as a REG_EXPAND_SZ registry value. Any values of type REG_SZ will not be expanded when read from the registry.

Additional remark: Whenever a process changes its environment (rather than the registry settings which define the environment for new processes), the changes are only visible for child processes.

Axel Kemper

Posted 2013-07-29T20:26:55.990

Reputation: 2 892

This doesn't seem to work (prior to a reboot, at least). The path variable is not in the HKCU\Environment, the path variable in cmd.exe shows correctly, but launched applications are still seeing the old path variable. – Martin Argerami – 2017-02-11T18:06:54.380

@Martin: yes, launched application do not recognize registry changes, because they get their memory copy of the environment during application startup. This copy depends on the parent process which starts the sub-processes. If they are for instance started from Windows Explorer, you'd have to restart Explorer to get the new PATH. – Axel Kemper – 2017-02-11T22:23:45.113

You nailed it on the head! The Path in there was adding some programs that I knew had been removed, such as Microsoft SQL. I knew it wasn't coming from the system properties path I had set. Thank you very much - from the future - you posted this answer more than 3 years ago. – SDsolar – 2017-07-04T00:25:33.170

1

I would like to emphasise Axel's point if you need to modify the Environmental Variables, then you need to make the change in the registry. Actually I created a PowerShell function to help with this: http://www.computerperformance.co.uk/powershell/powershell_env_path.htm

– Guy Thomas – 2013-07-30T12:18:18.187

1There are no values to be modified, only two variables tmp and temp. Are you sure that this is the path for user, that should be modified? – Johnny_D – 2013-09-19T09:42:48.580