Where are my environment variables set on Vista32? - windows-vista

By following:
Start -> Computer -> Right Click -> Properties -> Advanced System Settings -> Advanced -> Environment Variables
I find User variables for brianjd (which includes TMP and TEMP) and System Variables (which includes, Path, USERNAME, etc.). I would expect these variables to be set in C:\AUTOEXEC.BAT or C:\CONFIG.SYS. Instead, I find REM Dummy file for NTVDM and FILES=40 in those files.
Q: Where are the files that actually set the environment variables with statements like SET USERNAME=brianjd?
Q: When I follow the Windows GUI path above and add the variable name and value FOO and C:\foo to either User variables for brianjd or System Variables, what is actually happening? What is Windows writing to where?

The variables are not in a script file like /etc/environment on Linux, for example. Windows stores them in the registry. The keys are "HKCU\Environment" (user variables) and "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" (system-wide variables).
See also the relevant MSDN article.

Related

Set external computer IP to environment variable

I have an external computer with an IP address like 100.12.5.7 and I would like to set a path to the environment variable like so:
setx MEDIA \\\100.12.5.7\d\media\
When I tried the above command, cmd prompt me that the variable is set successfully but when I do an echo %MEDIA%, it only shows me %MEDIA% instead of the full path. Any idea how I could fix this?
Setx doesn't affect the current console. Use Set and Setx together. Set, unlike Setx, requires an equal sign.
The reason setx can't affect the current console is that a copy of the starting program's environment is passed to programs it starts (usually Explorer's environment either direct or indirect via CMD). So all changes are local to that program. Setx sets registry keys, so only new programs can read them, and that really mean CMD.exe, as that rereads the registry keys.

Why are "Path" Windows's environment variable and the command line different?

On my Pc, running Windows XP 5.1.2600, following
"My Computer" > "Properties" > "Advanced" > "Environment Variables" > "Path"
I get a string, but in a batch file, or directly typing from prompt, by means of
echo %path%
I get a different one.
Can someone, please, explain what makes them differ and how to fix the issue, setting the "DOS-path" to the same value of "Windows-PATH" ?
Two things:
As others have mentioned, the full value of PATH is the combination of the user's PATH variable and the system's PATH variable.
If you change environment variables (including PATH), any processes already running (including the Command Prompt) will continue using the old environment variable values. You will need to restart those processes to pick up any changes that you've made.
The value of the PATH environment variable you see on the command line (which has nothing to do with "DOS" btw) is the sum of the system wide PATH variable and the user specific PATH variable.
If you open the dialog with the system variables from within the control panel, the upper part contains the user specific environment variables. The lower part contains the system variables.
If you set a PATH variable for your user to e.g. c:\foo, and the system path is be c:\Windows\System32 then you'd see c:\Windows\System32;c:\foo when you do an ECHO %PATH% on the commandline.
Probably the difference you are seeing is that when a new process is made, its default environment contains a PATH variable that is made by merging the system variable with the user variable.
As for how to "fix the issue", there is nothing to fix. The system behaves as designed.
I was getting the same issue where the System Variable that I get through the
"My Computer" > "Properties" > "Advanced" > "Environment Variables" >
"Path"
was different that the one being displayed through the command line interface via
ECHO %PATH%
The fix for this is basically to wait for the change to take place and this can be made faster by restarting the cmd and explorer. To do that you can simply end task for Explorer and CMD and then start a new task or restart it directly.
The same shall be done for the CMD as well.
The Windows registry can hold path entries that do not show up in the Environment Variables version of PATH you see via the control panel. (Why, I do not know).
The difference means that something (likely an install script) made changes to the registry.
To view or change the local user's path variable, look in the HKEY_CURRENT_USER\Environment key.
To view the system path, look in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

What is the difference between the Environmental Variables for Windows System and PowerShell?

We set the Windows System Path variable via System > Advanced System Settings > Environmental Variables > Path > Edit.
We set the PowerShell ones via Env:Path += ";Some\New\Path"
What, if any, is the difference between these two?
When you set the variable in powershell, it's set in that process (and its children) only. This is equivalent to using SET from the cmd prompt or a batch file.
In the dialog, you are setting the variables for the user (or for the system), which affects every process spawned under those contexts. This is analogous to using SETX from the cmd prompt or a batch file.

PowerShell Enviromental Variable not seen?

Set by another Program (started as user) my own Environmental Variable using (kernel32.dll:
SetEnvironmentVariableW("ToTestVar_001", "ToTestVar.001 11:11:54" )
So the program that sets the Environmental Variable ToTestVar_001 can read it but I do In PowerShell (started as the same user)
Get-ChildItem Env:
ToTestVar_001 is not listed how can I create an Environmental Variable that can be read and written by my program and PowerShell?
My next try was to create an Env. Var. by PowerShell: "TestVariable"="test Value" but my program can't find it using kernel32.dll:
string rdBuff = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx":
GetEnvironmentVariableW("TestVariable", rdBuff, StringLen(rdBuff));
Where can I find, read and set the Kernel32.dll-Env.Variables in Win 7 64 bit - is that the problem?
Thanks in advance,
Gooly
This is not so simple.
In the way you are writting your code Get-ChildItem Env: will see the environment var positionned by your EXE file using SetEnvironmentVariableW("ToTestVar_001", "ToTestVar.001 11:11:54" ) only if your process (the EXE file) start PowerShell.exe (allowing his children to inherit his environment vars) after positionning the environment variable. In this case PowerShell will inherit of the environment vars and will see it.
It exists another place to set environment vars, the one in the registry I mean the one you can see using advanced system properties :
The environment variables for all users are all stored under:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
The environment variables for a specific user are all stored under:
HKEY_CURRENT_USER\Environment
But once again it's not so simple, if you change the registry and start a process all is ok, the var you set in registry can be seen in the process. But if the process is started when you add the var in the registry, then the process don't see the new environment var except if it manage a special event broadcast by the system informing that the environment vars have been modified (that is the way Explorer.exe is working).

Windows batch, select ONLY user variables

In environment variables I have a PATH variable for both user variables and system variables.
In a batch script, in order for me to append the user PATH variable with a new given path, I need to select the current value. Unfortunately %PATH% returns a combination of the user variable and the system variable.
Of course I only want to add a new custom path value to the user variable. There is no point in enhancing it with the system path as well. That's why I have 2 variables.
Thanks in advance.
Edit: Found in the documentation the following statement:
The %PATH% variable is set as both a system and user variable, the 2 values are combined to give the PATH for the currently logged in user....
Example:
User variables:
PATH
value: c:\dev
System variables
PATH
value: c:\Program Files
What I want to do, is to add to the user variable the value: c:\tmp, so that in the end PATH will have the value: c:\dev;c:\tmp
But, if open a cmd window:
echo %PATH%
c:\Program Files;c:\dev
so the setx will do the following
setx path "%path%;c:\tmp"
open new cmd
echo %PATH%
c:\Program Files;c:\dev;c:\tmp
And that is wrong, because I only needed c:\dev;c:\tmp
I hope I was more clear this time.
How are you modifying the variables?
There is only one environment variable PATH, so you can go ahead and change it. These changes are transient (and local to your process and its children).
There are two (actually more) persistent places in Registry from which the environment variables are initialized when a process is created. You can modify them using reg utility. There is no ambiguity since they are separate:
HKEY_CURRENT_USER\Environment
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
You may have to re-login for changes in Registry to take effect (I don't remember whether there's a programmatic way to notify explorer that these settings have changed). Also note that by default child processes inherit the environment of their parent (unless the parent takes special measures to do otherwise), so e.g. if you launch a cmd window and later modify the environment via system settings dialog, applications started from that cmd won't see the changes.
[UPD] You can get the value of user-specific environment variable from registry using reg utility:
reg query HKCU\Environment /v PATH
Though you'll have to filter its output for the actual value, as it spits out some useless text. Here an example incantation:
for /f "usebackq tokens=2,*" %A in (`reg query HKCU\Environment /v PATH`) do set value=%B
It will store the result in the environment variable value. Remember to double %'s when using it in a batch file.
If I understand your question, you have 2 %PATH% variables. One system one, and one user one, (presumably you created the latter yourself).
I have tried this and it seems to work for user environment variables
setx /s computername PATH %PATH%;newpathvalue
When I tested this I actually substituted PATH for a new var to make sure it works, but it may be best making a copy of your existing vars before doing this, just in case.
It will tag onto the end of the existing user environment variable PATH with the new value you specify, separating it from the others with a semi-colon ;.
Hope this helps

Resources