Skip to content

Instantly share code, notes, and snippets.

@cumulus13
Created December 18, 2025 11:45
Show Gist options
  • Select an option

  • Save cumulus13/b57f2348c65f03412c8a347a80cf1925 to your computer and use it in GitHub Desktop.

Select an option

Save cumulus13/b57f2348c65f03412c8a347a80cf1925 to your computer and use it in GitHub Desktop.
set windows os environmen path batch script to temporary or permanent
:: File: pathx.bat
:: Author: Hadi Cahyadi <cumulus13@gmail.com>
:: Date: 2025-12-18
:: Description:
:: License: MIT
@echo off
chcp 65001 >nul
:: Define ANSI color codes with proper escape character
for /f %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
set "RESET=%ESC%[0m"
set "GREEN=%ESC%[32m"
set "YELLOW=%ESC%[33m"
set "RED=%ESC%[31m"
set "CYAN=%ESC%[36m"
set "MAGENTA=%ESC%[35m"
:: Initialize variables
set "PERMANENT=0"
set "OPERATION=add"
set "TARGET_PATH="
:: Parse arguments
:parse_args
if "%~1"=="" goto validate_args
if /i "%~1"=="-p" (
set "PERMANENT=1"
shift
goto parse_args
)
if /i "%~1"=="--perm" (
set "PERMANENT=1"
shift
goto parse_args
)
if /i "%~1"=="-r" (
set "OPERATION=remove"
shift
goto parse_args
)
if /i "%~1"=="--remove" (
set "OPERATION=remove"
shift
goto parse_args
)
if /i "%~1"=="-h" goto usage
if /i "%~1"=="--help" goto usage
:: This should be the path
if not defined TARGET_PATH (
set "TARGET_PATH=%~1"
shift
goto parse_args
)
:: Extra argument
echo %RED%Error: Too many arguments%RESET%
goto usage
:validate_args
if not defined TARGET_PATH goto usage
:: Execute operation
if "%OPERATION%"=="add" goto handle_add
if "%OPERATION%"=="remove" goto handle_remove
:handle_add
:: Validate the path exists
if not exist "%TARGET_PATH%" (
echo %RED%Error: Path does not exist: %TARGET_PATH%%RESET%
exit /b 1
)
:: Check if path already exists
call :path_exists "%TARGET_PATH%"
set "path_check_result=%errorlevel%"
if %path_check_result% equ 1 (
echo %YELLOW%Warning: Path already exists in PATH: %TARGET_PATH%%RESET%
exit /b 0
)
:: Add to PATH
if %PERMANENT% equ 1 (
call :add_permanent "%TARGET_PATH%"
) else (
call :add_temporary "%TARGET_PATH%"
)
exit /b %errorlevel%
:handle_remove
:: Check if path exists
call :path_exists "%TARGET_PATH%"
set "path_check_result=%errorlevel%"
if %path_check_result% equ 0 (
echo %YELLOW%Warning: Path not found in PATH: %TARGET_PATH%%RESET%
exit /b 0
)
:: Remove from PATH
if %PERMANENT% equ 1 (
call :remove_permanent "%TARGET_PATH%"
) else (
call :remove_temporary "%TARGET_PATH%"
)
exit /b %errorlevel%
:add_temporary
set "path_to_add=%~1"
endlocal
set "PATH=%path_to_add%;%PATH%"
setlocal enabledelayedexpansion
echo !GREEN!Success: Path added temporarily (current session only)!RESET!
echo !CYAN!Added: %path_to_add%!RESET!
echo.
echo !YELLOW!Tip: Use -p flag to make changes permanent!RESET!
exit /b 0
:add_permanent
set "path_to_add=%~1"
echo %CYAN%Adding path permanently to User PATH...%RESET%
:: Get current User PATH from registry
for /f "skip=2 tokens=3*" %%a in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "USER_PATH=%%a %%b"
set "USER_PATH=%USER_PATH:~0,-1%"
:: Check if already exists in User PATH
echo %USER_PATH% | findstr /i /c:"%path_to_add%" >nul
if %errorlevel% equ 0 (
echo %YELLOW%Warning: Path already exists in User PATH registry%RESET%
exit /b 0
)
:: Add to User PATH (at the beginning)
if defined USER_PATH (
set "NEW_PATH=%path_to_add%;%USER_PATH%"
) else (
set "NEW_PATH=%path_to_add%"
)
:: Update registry
reg add "HKCU\Environment" /v PATH /t REG_EXPAND_SZ /d "%NEW_PATH%" /f >nul 2>&1
if %errorlevel% neq 0 (
echo %RED%Error: Failed to update User PATH in registry%RESET%
echo %YELLOW%You may need to run as Administrator%RESET%
exit /b 1
)
:: Update current session
endlocal
set "PATH=%path_to_add%;%PATH%"
setlocal enabledelayedexpansion
:: Broadcast WM_SETTINGCHANGE to notify other applications
powershell -NoProfile -Command "[Environment]::SetEnvironmentVariable('PATH', '%NEW_PATH%', 'User')" >nul 2>&1
echo !GREEN!Success: Path added permanently to User PATH!RESET!
echo !CYAN!Added: %path_to_add%!RESET!
echo.
echo !MAGENTA!Note: New Command Prompt windows will have this path automatically!RESET!
echo !YELLOW!Current session has been updated as well.!RESET!
exit /b 0
:remove_temporary
set "path_to_remove=%~1"
:: Normalize the path to remove
set "path_to_remove_norm=%path_to_remove%"
if "%path_to_remove_norm:~-1%"=="\" set "path_to_remove_norm=%path_to_remove_norm:~0,-1%"
:: Build new PATH manually, excluding the target path
setlocal enabledelayedexpansion
set "NEW_PATH="
set "first=1"
for %%p in ("%PATH:;=" "%") do (
set "current=%%~p"
if defined current (
:: Normalize current path
set "current_norm=!current!"
if "!current_norm:~-1!"=="\" set "current_norm=!current_norm:~0,-1!"
:: Compare case-insensitive
set "skip=0"
if /i "!current_norm!"=="%path_to_remove_norm%" set "skip=1"
:: Add to NEW_PATH if not skipped
if !skip! equ 0 (
if !first! equ 1 (
set "NEW_PATH=!current!"
set "first=0"
) else (
set "NEW_PATH=!NEW_PATH!;!current!"
)
)
)
)
:: Update PATH
endlocal & set "PATH=%NEW_PATH%"
setlocal enabledelayedexpansion
echo !GREEN!Success: Path removed temporarily (current session only)!RESET!
echo !CYAN!Removed: %path_to_remove%!RESET!
echo.
echo !YELLOW!Tip: Use -p flag to remove from permanent PATH!RESET!
exit /b 0
:remove_permanent
set "path_to_remove=%~1"
echo %CYAN%Removing path permanently from User PATH...%RESET%
:: Get current User PATH from registry
for /f "skip=2 tokens=3*" %%a in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "USER_PATH=%%a %%b"
set "USER_PATH=%USER_PATH:~0,-1%"
:: Check if exists in User PATH
echo %USER_PATH% | findstr /i /c:"%path_to_remove%" >nul
if %errorlevel% neq 0 (
echo %YELLOW%Warning: Path not found in User PATH registry%RESET%
exit /b 0
)
:: Remove using PowerShell
for /f "usebackq delims=" %%p in (`powershell -NoProfile -Command "'%USER_PATH%' -split ';' | Where-Object { $_ -and ($_.TrimEnd('\') -ne '%path_to_remove%'.TrimEnd('\')) } | Select-Object -Unique | Join-String -Separator ';'"`) do set "NEW_PATH=%%p"
:: Update registry
reg add "HKCU\Environment" /v PATH /t REG_EXPAND_SZ /d "%NEW_PATH%" /f >nul 2>&1
if %errorlevel% neq 0 (
echo %RED%Error: Failed to update User PATH in registry%RESET%
echo %YELLOW%You may need to run as Administrator%RESET%
exit /b 1
)
:: Update current session
for /f "usebackq delims=" %%p in (`powershell -NoProfile -Command "$env:PATH -split ';' | Where-Object { $_ -and ($_.TrimEnd('\') -ne '%path_to_remove%'.TrimEnd('\')) } | Select-Object -Unique | Join-String -Separator ';'"`) do (
endlocal
set "PATH=%%p"
setlocal enabledelayedexpansion
)
:: Broadcast WM_SETTINGCHANGE
powershell -NoProfile -Command "[Environment]::SetEnvironmentVariable('PATH', '%NEW_PATH%', 'User')" >nul 2>&1
echo !GREEN!Success: Path removed permanently from User PATH!RESET!
echo !CYAN!Removed: %path_to_remove%!RESET!
echo.
echo !MAGENTA!Note: Changes will apply to new Command Prompt windows!RESET!
echo !YELLOW!Current session has been updated as well.!RESET!
exit /b 0
:path_exists
setlocal enabledelayedexpansion
set "check_path=%~1"
:: Normalize path for comparison
set "check_path_norm=%check_path%"
if "!check_path_norm:~-1!"=="\" set "check_path_norm=!check_path_norm:~0,-1!"
:: Check in current PATH
set "found=0"
for %%p in ("%PATH:;=" "%") do (
set "current=%%~p"
if defined current (
if "!current:~-1!"=="\" set "current=!current:~0,-1!"
if /i "!current!"=="!check_path_norm!" set "found=1"
)
)
endlocal & exit /b %found%
:usage
echo.
echo %CYAN%%ESC%[1mPATH Manager - Temporary ^& Permanent PATH Management%RESET%
echo.
echo %ESC%[1mUSAGE:%RESET%
echo %~n0 [OPTIONS] ^<path^>
echo.
echo %ESC%[1mOPTIONS:%RESET%
echo -p, --perm Make changes permanent (User PATH registry)
echo -r, --remove Remove path instead of adding
echo -h, --help Show this help message
echo.
echo %ESC%[1mEXAMPLES:%RESET%
echo %GREEN%# Add temporary (current session only)%RESET%
echo %~n0 "C:\msys64\mingw64\bin"
echo.
echo %GREEN%# Add permanent (saved to registry)%RESET%
echo %~n0 -p "C:\msys64\mingw64\bin"
echo.
echo %GREEN%# Remove temporary%RESET%
echo %~n0 -r "C:\msys64\mingw64\bin"
echo.
echo %GREEN%# Remove permanent%RESET%
echo %~n0 -p -r "C:\msys64\mingw64\bin"
echo %~n0 -r -p "C:\msys64\mingw64\bin"
echo.
echo %ESC%[1mNOTES:%RESET%
echo %YELLOW%- Default: Temporary (affects current session only)%RESET%
echo %YELLOW%- Permanent: Modifies User PATH in registry%RESET%
echo %YELLOW%- New paths are added at the beginning of PATH%RESET%
echo %YELLOW%- Permanent changes require restarting Command Prompt%RESET%
echo.
exit /b 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment