I am trying to add C:\\xampp\\php
to my system PATH
environment variable in Windows.
I have already added it using the Environment Varia
Nod to all the comments on the @Nafscript's initial SETX answer.
SETX
by default will update your user path.SETX ... /M
will update your system path.%PATH%
contains the system path with the user path appendedPATH
- SETX
will truncate your junk longer than 1024 charactersSETX %PATH%;xxx
- adds the system path into the user pathSETX %PATH%;xxx /M
- adds the user path into the system pathThe ss64 SETX page has some very good examples. Importantly it points to where the registry keys are for SETX
vs SETX /M
User Variables:
HKCU\Environment
System Variables:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
PATH
append_user_path.cmd
@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 %CurrPath% > user_path_bak.txt
SETX PATH "%CurrPath%";%1
PATH
append_system_path.cmd
. Must be run as administrator.
(It's basically the same except with a different Key
and the SETX /M
modifier.)
@ECHO OFF
REM usage: append_system_path "path"
SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath% > system_path_bak.txt
SETX PATH "%CurrPath%";%1 /M
Finally there's potentially an improved version called SETENV recommended by the ss64 SETX page that splits out setting the user or system environment variables.
1. Not strictly true
You don't need any set
or setx
command. Simply open the terminal and type:
PATH
This shows the current value of PATH variable. Now you want to add directory to it? Simply type:
PATH %PATH%;C:\xampp\php
If for any reason you want to clear the PATH variable (no paths at all or delete all paths in it), type:
PATH ;
Update
Like Danial Wilson noted in comment below, it sets the path only in the current session. To set the path permanently, use setx
but be aware, although that sets the path permanently, but not in the current session, so you have to start a new command line to see the changes. More information is here.
To check if an environmental variable exist or see its value, use the ECHO command:
echo %YOUR_ENV_VARIABLE%
Aside from all the answers, if you want a nice GUI tool to edit your Windows environment variables you can use Rapid Environment Editor.
Try it! It's safe to use and is awesome!
Backup my current PATH environment variable: run cmd
, and execute command: path >C:\path-backup.txt
Get my current path value into C:\path.txt file (same way)
setx path "Here you should insert string from buffer (new path value)"
php -v
To override already included executables;
set PATH=C:\xampp\php;%PATH%;
A better alternative to Control Panel is to use this freeware program from SourceForge called Pathenator.
However, it only works for a system that has .NET 4.0 or greater such as Windows 7, Windows 8, or Windows 10.