Using setx to append to user PATH

My attempt:

setx PATH "%PATH%;%~dp0"

How do I do this without everything in system PATH getting duplicated in user PATH?

1

3 Answers

How do I do this without the system PATH getting duplicated in user PATH?

Use the /m option.

setx /m PATH "%PATH%;%~dp0"

Notes:

  • /m - Set the variable in the system environment HKLM. (The default is the local environment HKCU)

setx usage

F:\test>setx /?
SetX has three ways of working:
Syntax 1: SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]
Syntax 2: SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]
Syntax 3: SETX [/S system [/U [domain\]user [/P [password]]]] /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
Description: Creates or modifies environment variables in the user or system environment. Can set variables based on arguments, regkeys or file input.
Parameter List: /S system Specifies the remote system to connect to. /U [domain\]user Specifies the user context under which the command should execute. /P [password] Specifies the password for the given user context. Prompts for input if omitted. var Specifies the environment variable to set. value Specifies a value to be assigned to the environment variable. /K regpath Specifies that the variable is set based on information from a registry key. Path should be specified in the format of hive\key\...\value. For example, HKEY_LOCAL_MACHINE\System\CurrentControlSet\ Control\TimeZoneInformation\StandardName. /F file Specifies the filename of the text file to use. /A x,y Specifies absolute file coordinates (line X, item Y) as parameters to search within the file. /R x,y string Specifies relative file coordinates with respect to "string" as the search parameters. /M Specifies that the variable should be set in the system wide (HKEY_LOCAL_MACHINE) environment. The default is to set the variable under the HKEY_CURRENT_USER environment. /X Displays file contents with x,y coordinates. /D delimiters Specifies additional delimiters such as "," or "\". The built-in delimiters are space, tab, carriage return, and linefeed. Any ASCII character can be used as an additional delimiter. The maximum number of delimiters, including the built-in delimiters, is 15. /? Displays this help message.
NOTE: 1) SETX writes variables to the master environment in the registry. 2) On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window. 3) On a remote system, variables created or modified by this tool will be available at the next logon session. 4) The valid Registry Key data types are REG_DWORD, REG_EXPAND_SZ, REG_SZ, REG_MULTI_SZ. 5) Supported hives: HKEY_LOCAL_MACHINE (HKLM), HKEY_CURRENT_USER (HKCU). 6) Delimiters are case sensitive. 7) REG_DWORD values are extracted from the registry in decimal format.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • setx - Set environment variables permanently, SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU).
2

[CMD, WMIC, HKLM, HKCU, SETX, PATH, 2001]

the problem is probably more than 20 years old:

- setx Path "%Path%;NewPath" pollutes the user path with the system path.

- setx Path "%Path%;NewPath" /M pollutes the system path with the user path.

- the pollution: video

I am not sure if there is a safe native solution that contemplate NON-ANSI directories making exclusive use of the command prompt.

this is my attempt!

- using Microsoft Windows [Version 10.0.19042.868]

0 - check the paths

echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='Path'" get variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue

tip: wmic ENVIRONMENT get * [to see more]

info: a registry backup was performed using the above commands. the files have been saved in the current directory.

1 - backup user path at %USERPATH%

- the commands pre-suppose:

  • non-null path;
  • functional path;

- if null or unfunctional path do only step "2"

wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %temp%\userpathvalue.txt

info: there is some pollution* at the end of the string.

set /P USERPATH=<%temp%\userpathvalue.txt

- add '/' to better see spaces

set "USERPATH=%USERPATH%/"

info

- remove up to 15 end spaces, then remove '/'

set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"

info: the number of spaces introduced was NOT consistent in my tests.

echo %USERPATH%[checking final string]
setx USERPATH "%USERPATH%"

* when dealing with invisible characters it is easier to identify them by converting them to hexadecimal, do:

wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %tmp%\0 && set /P TEMPREADER=<%tmp%\0
echo %TEMPREADER% >%tmp%\1 && echo %TEMPREADER%/ && certutil -f -encodehex "%tmp%\1" "%tmp%\2" 11 | more +4 && type "%tmp%\2"
REM to open on notepad, type: certutil -f -encodehex "%tmp%\1" "%tmp%\2" 5 && cmd /C START "" /MAX notepad %tmp%\2

tip: use 4, 5, 10, 11 (same as none) in encodehex

see

2 - set¹²³ user path as %USERPATH% only

setx PATH ^%USERPATH^%

done!

¹ info: setx PATH "%USERPATH%" sets the variable value instead of the literal %USERPATH%;

² info: using the literal is better, because the behavior becomes dynamic instead of static. i.e., by using the literal instead of the value, only the %USERPATH% variable needs to be changed in the future. whereas, by using the value (i.e., the addresses present inside the variable), the %PATH% user variable must also be updated upon change in %USERPATH% [which is not desirable].

³ info: below methods NOT recommended! it works, but it is easier to make mistakes.

reg add "HKEY_CURRENT_USER\Environment" /v PATH /d ^%USERPATH^% /f
wmic ENVIRONMENT set name="PATH", variablevalue=^%USERPATH^%, username="%COMPUTERNAME%\\%USERNAME%"

3 - for now on just use %USERPATH%

setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"

tip: close all programs, especially terminals, before continuing.

4 - re-check

echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='path'" get variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='path'" get variablevalue

5 - visual check

rundll32 sysdm.cpl,EditEnvironmentVariables

info: the system PATH can only be changed if the previous command was launched with administrative privilege;

6 - TL;DR: do it! / or don't!

REM the commands pre-suppose
REM - non-null path;
REM - functional path;
REM if null or unfunctionl path don't progress!
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %temp%\userpathvalue.txt
set /P USERPATH=<%temp%\userpathvalue.txt
set "USERPATH=%USERPATH%/"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"
echo %USERPATH%[checking final string] && echo %USERPATH% > %tmp%\0
certutil -f -encodehex "%tmp%\0" "%tmp%\1" 11 | more +4 && type "%tmp%\1"
setx USERPATH "%USERPATH%"
setx PATH ^%USERPATH^%
echo done!
REM setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"
REM please close all programs

cheers!

It looks like this page may cover what you are trying to do:adding PATH with SETX or PATHMAN or something else?Or maybe add more detail to your question, so we can understand how this is different. However, that page has multiple solutions to either the same or a similar problem as to what you are asking.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like