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

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

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.

need SETX /M, default SETX set to HKEY_CURRENT_USER

SETX /M PATH c:\my-bin-path;%PATH%