2

I want to know, how to to append value to existing PATH variable (user scope):

SETX PATH "%PATH%;D:\Myfolder\Test\"

The above script takes system path variable data using %PATH% and append my folder path (D:\Myfolder\Test\) to PATH (user scope).

My need is to take user path variable before append. I need a fix for this case.

Please provide your suggestion.

1 Answer 1

2

Just making sure I understand correctly: you want to edit the user-scope PATH variable, but don't want to add all the stuff from the system-scope copy of PATH to your user-scope one?

The best way I know of to do this is actually to use Powershell. Yes, I know you tagged this as bash, but I'm pretty sure you didn't mean to; setx is a Windows command. :-)

[System.Environment]::GetEnvironmentVariable("PATH","USER") will get the current-user-scope PATH environment variable as a string. You can then append to this and store it back using the SetEnvironmentVariable function.

$userpath = [System.Environment]::GetEnvironmentVariable("PATH","USER")
$userpath = $userpath + ";D:\Myfolder\Test\"
[System.Environment]::SetEnvironmentVariable("PATH",$userpath,"USER")

You can read more about messing with environment variables at this SuperUser question.

Note that, like the setx command, this won't actually change any environment variables in the current process. If you want to do that, you can use "PROCESS" instead of "USER" in a call to SetEnvironmentVariable, or just append your value to $ENV:PATH, something like $ENV:PATH = $ENV:PATH + ";D:\Myfolder\Test\"

2
  • Good understanding. I don't know about Powershell script .Shall we create a Powershell script file which contains these scripts (your 3 lines) and single click on this file to achieve our functionality.Is there such any possibility? Sep 22, 2015 at 9:40
  • You would need to alter the script trust rules (or sign the script) for it to just work by double-clicking a .PS1 file, but you could. You could also use the -Command argument to Powershell, similar to the /C argument in CMD, to tell it to run just one particular command. You could combine the three lines into a single line (either by nesting commands, or by separating with ; semicolon characters). You could then tell PS to run that whole thing with one line of CMD.
    – CBHacking
    Sep 22, 2015 at 19:11

You must log in to answer this question.

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