Windows – How to update the PATH user environment variable from command-line

command lineenvironment-variablespathwindows

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.

Best Answer

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)"
Related Question