How are Windows Environment variables evaluated? - windows

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?

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.

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.

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

Related

Is there any API to anticipate an exe not being reachable via Path environment variable

So I need to triage my PATH environment variable and add extra folders so it can find a certain executable. I was wondering is there a way to mimic how the Windows OS walks all the directories in the path to see if it "reaches" an exe?
I know I could write a program but ideally there is a Windows API call.
PathFindOnPath takes in a file name and returns the fully qualified file path, if the file is found. Note, however, that it also looks in standard directories, in addition to PATH.

Is there a way to call an external program that could affect current terminal's environment variable?

I write a program which is run under the terminal on windows, it needs to specify the PATH so that it could be called without full absolute path. However, I have specified the "PATH" and save the value to the "users'" environment variables. However, this doesn't take effect unless I reboot the terminal. If I manually set the PATH like this:
PATH=%PATH%;D:\folder
The terminal could find the executable program under the D:\folder, if I call the set in an external program, it seems it doesn't affect current terminal.(Maybe the external program is the child progress of current terminal which the PATH only exists for current session).
I already tried "set PATH=xxx" and "PATH=xxx", none works.
So the problem is how can I reset current terminal session's envionment by an external program? I don't want to manually set the PATH in the terminal, this would be tedious for the user.
Is there a way to do so?
BTW, I noticed this Setting Windows PowerShell path variable, after I called "setx PATH D:\folder -m", it shows that the operation was success, but I output the $PATH and the value doesn't include D:\folder
Look at this link: http://technet.microsoft.com/de-ch/magazine/2008.10.windowspowershell.aspx
Maybe you can setup this profile and do the PATH=%PATH%;D:\folder there. The profile is loading every time you start powershell.exe.
To answer the exact question you've asked, no, there is no way for an invoked program to alter the callers environment. If there was, I'd imagine there would be all kinds of crazy ways to turn that into a security nightmare.
The calling program could decide it explicitly wants to get values from the called process.
There is a slightly hacky (but effective) way of doing this using this script: http://poshcode.org/2176
Alternatively, #Ansgar linked to another possibility.

How to install to current user path?

I'm making an installer and I want it to extract to a specific user path like C:\Current User\Documents with current user being the current user installing it. Anyway to do this?
The most important thing is to never hard-code a path like C:\Current User\Documents or C:\Documents and Settings\username, because the actual folder names will vary depending on the Operating System and language of the user's computer.
In most installers, you can use a pre-defined command-line variable for various OS defined folders. For example, the variable %HOMEDRIVE% points to the default driver letter such as C:\, and %HOMEPATH% is usually the profile folder. So, on my computer,
%HOMEDRIVE%\%HOMEPATH% = C:\Users\username
The command-line variable %USERPROFILE% points to the same location. If you just need the username, use %USERNAME%.
If you are looking for the user's "My Documents" folder or other similar folders, you can get it from the registry:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

Why is %appdata% and other variable not visible in window's gui

I have searched for hours trying to find a reason why certain environment variables are visible in control-panel>system>advance-system-settings>environment-variables. Where are the other variables set and why are they not visible here?
Windows stores the location of per-user special folders under the following Registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
The common special folders are specified in the same path under HKEY_LOCAL_MACHINE instead:
HKEY_LOCAL MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
As for why they're not exposed in the GUI like other user variables, I can only speculate. But my guess is that it's because they're not really intended to be changed by the user. The paths are determined by the system, and are intended for internal use by applications.
The variables that are shown in the GUI dialog are the ones that the user can modify.
Others like APPDATA have values that are determined by the system and so Windows doesn't show them in the dialog that allows you to edit them.
For the complete list of Windows environment variables including hidden ones take a look at Environment Variables Wiki: System variables, Hidden variables.
Hope that helps.

Whats the main need of Environment variable?

Hi what is the main need for setting an Environment Variable, while we have been installing many languages. What's there need? And does the installation cant set(in case of java)? Why so?
Environment variables are set to allow access to command line tools and to enable other tools to interact with SDKs more easily. For example, with Java on Windows, if the environment variable is not set on the PATH, running javac is much more cumbersome because you need to type in the full path to the command each time:
C:> \jdk<version>\bin\javac MyClass.java
In Java setting the environment variables isn't required; it's just easier. Other languages may be more stringent, though I haven't seen any specific examples I could cite. You can read the article How Do I Set the Path System variable? for specifics on how to do this.
The Java installer doesn't change the path variable, but other tools do (Microsoft's own, for example). I assume it's a design decision on the part of Sun/Oracle rather than any particular technical limitation.
In case of JAVA You can run the JDK just fine without setting the PATH variable, or you can optionally set it as a convenience. However, you should set the path variable if you want to be able to run the executables (javac, java, javadoc, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it.
windows make the Environment variable because of access and organize promotions to users and protection
hope this will h e l p you

Resources