Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.
share|improve this question
    
Seems to me that when you use ;%PATH% you're asking for the existing path (derived from the system path) to be appended. –  Daniel R Hicks May 28 '13 at 22:35

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
1  
The answer is very detailed, but I don't see how it answer the questions. To keep it short, the question is "How could I Update the User PATH from command-line"... but I will edit the question to make it clearer. Sorry if it was not. –  Christian Lemer May 29 '13 at 6:00
    
@Chris: Your edit clarifies what you really want to achieve, so I have updated my answer accordingly. –  Karan May 30 '13 at 14:00

need SETX /M, default SETX set to HKEY_CURRENT_USER

SETX /M PATH c:\my-bin-path;%PATH%
share|improve this answer
    
This affect the system PATH variable... maybe my example was misleanding, but I wold like to update the user PATH variable specifically. –  Christian Lemer May 28 '13 at 21:40
    
@Chris op111.net/82 –  STTR May 28 '13 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.