31

If I have a System and User environment variable with the same name, how are they processed? Are they concatenated? Does the user variable override the system variable? Taking that into account, if I need to add something to the Path variable, where is it more convenient to add it?

3 Answers 3

23

Everything splash says in their answer is correct. To be absolutely clear, there is a difference between how the user path environment variable is evaluated, and the other user environment variables are evaluated. A regular user environment variable overrides completely a system one with the same name if both exist, but only for the specific user it is specified for. However, the user path variables is treated differently. It is appended to the system path variable when evaluating, rather than completely replacing it. I believe splash states that, but they do it so concisely I think it needs spelling out.

22

I think this article should answer you question: Environment variables in Windows NT

User environment variables

User environment variables can be viewed from Control Panel as well. The user may add, delete or modify the environment variables in the User Environment Variables for User field. These variables take precedence over system environment variables. The user path is appended to the system path.

2
  • that article is erroneus. Try delete the %PATH% of the user path. Then everything that need the path don't going to work properly. I'm thinking that the user variables override the system variables. So if i have an empty user Path variable i'm going to have alot of problems to execute almost everything. To reestablish the value you could delete that variable or set it again with the %PATH%; prefix. You could use powershell: set-itemproperty hkcu:\Environment -name Path -value "%PATH%"
    – mjsr
    Commented Feb 26, 2011 at 14:33
  • 3
    @Ps1CsCpp, I don't think what you're saying is correct. The article states that user PATH entries are appended to system-wide PATH. For your scenario, an empty user PATH would have no effect on the PATH provided to a process. Said another way, adding an empty string to the end of the system PATH will have no effect. I have tested this on Windows 8. Commented Sep 3, 2013 at 18:37
19

Everything that splash and Simon say in their answers are 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 wish 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, left-to-right, with the user path 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 user path. (In fact, it must be this way, or certain programs would stop working correctly, and 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.

2
  • Great answer. However, why then does the context in which a program is started matter?In my case, to have Qt enabled in TexStudio when using PythonTex, I must start TexStudio from the Anaconda prompt and not the Cmd prompt. In both cases the environment variables and paths are the same!
    – Colin
    Commented Apr 29, 2020 at 1:22
  • That would be a good topic for a new question. (I've started using Anaconda myself within the past year, but am not sure of details on how environment variables are made available in that shell.) Commented Apr 29, 2020 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.