User vs. System Environment Variables: Do System Variables Override User Variables?

22

5

I have elevated rights to my laptop but not admin rights. Admin rights are required to modify system environment variables. I expected that if I created a user environment variable with the same name as a system environment variable, the user variable would override the system variable but this does not appear to be the case.

After adding a user variable of the same name as a system variable, I opened up a brand new cmd window and used the echo command to display the variable. It showed me the value of the system variable instead of the value of the user variable.

I just wanted to confirm this is expected behavior and understand the reasoning behind it. I would expect the more specific user setting to override the system one.

I have Windows 7.

Chad

Posted 2015-01-20T15:50:48.773

Reputation: 1 654

When you change variables you often have to completely restart Windows. Changing a variable in the settings will not change running processes. New processes copy the environment from running processes (depending on how they are started), so simply starting a process doesn't mean you get a new environment. – Zoredache – 2015-01-20T16:30:09.643

I rebooted and still when I run "echo %path%" from a cmd.exe window, it displays my system env variable, not the value of the user variable. So, I deleted the User variable, o Admin access, changed my System path variable and the did teh same echo cmd. The updated env value was displayed w/o rebooting. This tells me that USER vars do not override system vars and that a reboot isn't necessary. Neitehr is what I expected. – Chad – 2015-01-20T17:56:58.153

What procedure used for adding a user variable of the same name as a system variable? Something like?

– JosefZ – 2015-01-20T21:53:32.260

JosefZ: yes, I added the two environment variables, one USER the other SYSTEM, both with the same name, using teh Windows 7 GUI similar to the screens shown inyour link. – Chad – 2015-01-21T17:55:10.000

@Chad Isn't the answer you accepted below the exact opposite of your experience? How do you explain the discrepancy? – RockPaperLizard – 2015-09-05T01:02:11.413

Answers

26

According to the MSKB article Environment variables in Windows NT:

User environment variables....take precedence over system environment variables.

One notable exception is the PATH variable which is a combined result of the system and user variables:

The Path is constructed from the system path, which can be viewed in the System Environment Variables field in the System dialog box. The User path is appended to the system path.

The article also discusses identical exceptions for the expansion of the LibPath and Os2LibPath variables as well as how those specified in autoexec.bat are handled. These points are likely to find little relevance in today's typical environments.

Credit to this SO answer

I say Reinstate Monica

Posted 2015-01-20T15:50:48.773

Reputation: 21 477

3Is it possible to make User path prepending the System path? – Qwerty – 2018-10-05T20:57:32.283

3

Everything that Twisty Impersonator said in their answer is correct. The idea that the user path variable is appended has been highlighted, and I believe the consequences of that difference require some additional treatment.

Path = %Path% (System) ; %Path% (User)

When you execute an executable program (or any executable script, such as .bat, .vbs, etc.) you do not need to provide the fully qualified path.

For instance, to run java, you can type in any of these:

C:/Program Files (x86)/Java/jre6/bin/java -version

java.exe -version

java -version

The first example uses a fully qualified path. This will always use the version of the Java at that exact path.

The second example will go through each of the directories in the %Path% environment variable, looking for an executable file named java.exe. It will run the very first one that is found, and stop searching. If there are two files named java.exe somewhere on the %Path%, only the first one found is used.

The third example, like the second, will iterate over the directories listed in the %Path%. In addition, because a file extension was not provided, a list of executable file extensions are appended to the name of the file, in the order specified in the %PATHEXT% environment variable. If there are several files named java.com, java.exe, java.bat, etc. somewhere on the %Path%, only the first one found is used.

You can see the list of executable path extensions on your system by creating the following batch file:

@echo off
echo %PATHEXT%
pause

On my machine, these are:

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

What does all this mean?

In stark contrast to other environment variable, the user path does not allow you to override the system path. The exact opposite is the case. From the examples above, there are many cases where you may with to change the default version of Java. However, if there is already a Java version listed in the system path, that is the version that will ALWAYS be found first, because the path is searched in order, from left-to-right, and the user path is appended on the right-hand side, with the system path on the left.

What can I do about it?

If you do not have access to system environment variables, you cannot override default programs on the system path by using the the user path. (In fact, it must be this way, or certain programs would stop working correctly, and it would open your system to tampering by malicious software. Nobody wants that.)

Instead, you must use a fully qualified path if you must use a specific version.

JonathanDavidArndt

Posted 2015-01-20T15:50:48.773

Reputation: 890

1Is it possible to make User path prepending the System path? – Qwerty – 2018-10-05T20:57:35.553

1That would be a good topic for a different question. (A quick search on this site did not reveal anything immediately relevant.) Please feel free to ask a new question, and post a link to it here in the comments! – JonathanDavidArndt – 2018-10-08T01:00:28.647