I looked through a few solutions here and found an almost working script to append to the user Environment Variable PATH without overwriting the existing path and not clutter it with system's EV's PATH.
I wanted to add 4 paths, 2 from Python and 2 from Anaconda. The first 3 locations work perfectly (Python311, Python312, conda.exe), but it keeps duplicating the 4th path (_conda.exe) even though the script sequence is the same.
Thoughts?
@echo off
REM usage: append_user_path "path"
SET Key="HKCU\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %appdata%\Python\Python311\Scripts to PATH
@echo off
set "newPath=%appdata%\Python\Python311\Scripts"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
REM usage: append_user_path "path"
SET Key="HKCU\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %appdata%\Python\Python312\Scripts to PATH
@echo off
set "newPath=%appdata%\Python\Python312\Scripts"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
REM usage: append_user_path "path"
SET Key="HKCU\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %ProgramFiles%\Continuum\Anaconda3\conda.exe to PATH
@echo off
set "newPath=%ProgramFiles%\Continuum\Anaconda3\conda.exe"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
REM usage: append_user_path "path"
SET Key="HKCU\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
echo Add %ProgramFiles%\Continuum\Anaconda3\_conda.exe to PATH
@echo off
set "newPath=%ProgramFiles%\Continuum\Anaconda3\_conda.exe"
echo %CurrPath% | findstr /i /c:"%newPath%" >nul
if errorlevel 1 (
setx PATH "%CurrPath%;%newPath%"
echo Path updated successfully.
) else (
echo Path already contains the directory.
)
Pause
Append path works as intended for the first 3 paths if they already exist. But even if the 4th path exists, it appends/adds/duplicates it anyway:


%PATH%? AFAIK, only the directory path, not the executableset "newPath=%ProgramFiles%\Continuum\Anaconda3".%AppData%\Python\Python312\Scripts,%AppData%\Python\Python311\Scripts, and%ProgramFiles%\Continuum\Anaconda3, not the expanded variable form. The reason is that the variable registry entries are of the formREG_EXPAND_SZ, which is designed to expand variables when read. Also IMO, any executable file located within a directory accessible by all users, (Anaconda), should be added to the SystemPATH, not the UserPATH...%UserProfile%\AppData\Roaming\Python\Python311\Scripts,%Appdata%\Python\Python311\Scripts,%SystemDrive%\Users\tknguye\AppData\Roaming\Python\Python311\ScriptsandC:\Users\tknguye\AppData\Roaming\Python\Python311\Scripts, to cover each of the most likely existing entries for just that one location.