13

I have a system PATH variable with the system level config. I use the user PATH variable to complement the PATH with user-specific config.

I would like to update the user PATH variable from command-line for example with setx.

But I don't know how to reference the existing user path in setx.

In the following command (setx without /M)

setx PATH c:\my-user-specifc-bin-path;%PATH%

the first PATH means user PATH but the second %PATH% will be substituted by the "full" (user + system) PATH.

So it means that the entire system path would be duplicated in the user PATH... what is definitively not what I want.

I would like to:

  • Affect only the user PATH environment variable
  • Append/Prepend one or more path element to the existing value
  • Do it from the command-line.
1
  • Seems to me that when you use ;%PATH% you're asking for the existing path (derived from the system path) to be appended. May 28, 2013 at 22:35

4 Answers 4

4

PowerShell version, set PATH for user:

  1. Set new PATH (overwrite) for current user:
PS> Set-ItemProperty -path HKCU:\Environment\ -Name Path -Value "C:\MyPath1"
  1. Set append to current user PATH:
PS> Set-ItemProperty -path HKCU:\Environment\ -Name Path -Value "$((Get-ItemProperty -path HKCU:\Environment\ -Name Path).Path);C:\MyPath1"
  1. Set prepend to current user PATH:
PS> Set-ItemProperty -path HKCU:\Environment\ -Name Path -Value "C:\MyPath1;$((Get-ItemProperty -path HKCU:\Environment\ -Name Path).Path)"
2
  • Thank you very much! This works for me :) Two further questions: (1) In case C:\MyPath1 contains a whitespace, how do I escape it correctly? (2) In case I want to add multiple directories to Path, how do I specify multiple paths?
    – JavAlex
    Feb 28 at 16:23
  • @JavAlex, the quote already provide incase your path have space, e.g. -Value "C:\My folder\My Path 1"
    – Tho Ho
    Feb 29 at 3:48
14

To set the User PATH overwriting any previous value:

setx PATH "C:\MyPath1"

To prepend a value "C:\MyPath0" to the existing User PATH:

for /f "skip=2 tokens=3*" %a in ('reg query HKCU\Environment /v PATH') do @if [%b]==[] ( @setx PATH "C:\MyPath0;%~a" ) else ( @setx PATH "C:\MyPath0;%~a %~b" )

To append a value "C:\MyPath2" to the existing User PATH:

for /f "skip=2 tokens=3*" %a in ('reg query HKCU\Environment /v PATH') do @if [%b]==[] ( @setx PATH "%~a;C:\MyPath2" ) else ( @setx PATH "%~a %~b;C:\MyPath2" )

The if-else condition is required because the User PATH may or may not contain spaces. If you want you can save the commands as generic batch files (be sure to double each % sign) that accept the value to be set/prepended/appended as an argument.

Batch File

:: PATH-ADD - add a path to user path environment variable

@echo off
setlocal

:: set user path
set ok=0
for /f "skip=2 tokens=3*" %%a in ('reg query HKCU\Environment /v PATH') do if [%%b]==[] ( setx PATH "%%~a;%1" && set ok=1 ) else ( setx PATH "%%~a %%~b;%1" && set ok=1 )
if "%ok%" == "0" setx PATH "%1"

:end
endlocal
echo.
4
  • 2
    more explanation needed. what is %a, what is %b?
    – OfusJK
    Nov 1, 2014 at 2:40
  • Unfortunately it doesn't work if the user PATH is not already defined :(
    – nowox
    Jan 13, 2015 at 7:35
  • @KJK: for /? will tell you all you need to know. :) I didn't want to replicate it all here unnecessarily and thought that info would be easy to figure out anyway.
    – Karan
    Apr 5, 2015 at 2:53
  • The code does not check if user PATH already ends with a semicolon in which case appending passed directory should be done without an additional semicolon. But the biggest problem with this code is a corruption of user PATH if the string becomes longer than 1024 characters because of setx truncates the string to 1024 characters with displaying an appropriate information before adding it to Windows registry. So it could happen that instead of appending passed directory, the existing user PATH is truncated (garbled) by this code.
    – Mofi
    Jul 25, 2019 at 19:01
4

need SETX /M, default SETX set to HKEY_CURRENT_USER

SETX /M PATH c:\my-bin-path;%PATH%
3
  • This affect the system PATH variable... maybe my example was misleanding, but I wold like to update the user PATH variable specifically. May 28, 2013 at 21:40
  • @Chris op111.net/82
    – STTR
    May 28, 2013 at 22:23
  • Since %PATH% contains both the local path and system path entries this solution copies all the local path entries to the system path effectively duplicating them. Now those entries exist both in the system path and local path. The entire objective of the question was to avoid this duplication. If you leave off the /m you have the same problem but now you are duplicating the system PATH
    – JMS
    Jul 18, 2023 at 16:25
1

This seems to work:

setx PATH "c:\my-user-specifc-bin-path-which-may-contain-spaces;"%%PATH%%
1
  • 1
    It is an absolute NO GO, NEVER EVER to update user or system PATH using local PATH. This command line really corrupts user PATH by adding to Windows registry for user PATH the string c:\my-user-specifc-bin-path-which-may-contain-spaces;%C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;% in best case. Everyone using this command line has successfully corrupted the user PATH setting and so local PATH as used by all processes running with current user account after Windows restart or logoff/ logon.
    – Mofi
    Jul 25, 2019 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .