Windows, select ONLY user variables

In environment variables, I have a PATH variable for both user variables and system variables.

In the script package, so that I can add the PATH user variable with the new path given, I need to select the current value. Unfortunately,% PATH% returns a combination of a user variable and a system variable.

Of course, I want to add a new user path value to the user variable. There is no point also improving it using the system path. That is why I have 2 variables.

Thanks in advance.

Edit: the documentation contains the following statement:

The% PATH% variable is set by both the system and user variables, the values ​​of 2 are combined to give PATH for the current user.

Example:

User Variables:

PATH value: c:\dev 

System variables

 PATH value: c:\Program Files 

What I want to do is add a value to the user variable: c: \ tmp, so at the end of PATH it will have the value: c: \ dev; c: \ tmp

But if you open the cmd window:

 echo %PATH% c:\Program Files;c:\dev 

therefore setx will do the following

 setx path "%path%;c:\tmp" 

open new cmd

 echo %PATH% c:\Program Files;c:\dev;c:\tmp 

And this is wrong because I only need c: \ dev; c: \ tmp

Hope this time I was more clear.

+6
source share
2 answers

How do you change variables?

There is only one PATH environment variable, so you can continue and change it. These changes are temporary (and local to your process and its children).

There are two (actually more) permanent places in the registry from which environment variables are initialized when the process is created. You can change them using the reg utility. There is no ambiguity as they are separate:

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

You may need to re-register changes in the registry (I don’t remember if there is a software way to notify Explorer that these settings have changed). Also note that by default, child processes inherit their parent’s environment (unless the parent takes special measures to do otherwise), for example, if you launch the cmd window and then change the environment using the system’s settings dialog box, applications, started with this cmd will not see the change.

[UPD] You can get the value of a user environment variable from the registry using the reg utility:

 reg query HKCU\Environment /v PATH 

Although you will have to filter your output for the actual value as it splashes out some kind of useless text. Here is an example of a spell:

 for /f "usebackq tokens=2,*" %A in (`reg query HKCU\Environment /v PATH`) do set value=%B 

It will save the result in the environment variable value . Remember to double % when used in a batch file.

+7
source

If I understand your question, you have 2 %PATH% variables. One system and one user (presumably, you yourself created the latter).

I tried this and it seems to work for user environment variables

 setx /s computername PATH %PATH%;newpathvalue 

When I tested this, I actually replaced PATH with the new var to make sure it works, but it is best to make a copy of your existing vars before doing this, just in case.

It marks at the end of the existing user environment variable PATH new value you set, separating it from the rest with a colon ; .

Hope this helps

+1
source

All Articles