Reputation: 19347
I am trying to add C:\xampp\php
to my system PATH
environment variable in Windows.
I have already added it using the Environment Variables dialog box.
But when I type into my console:
C:\>path
it doesn't show the new C:\xampp\php
directory:
PATH=D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Files\Microsoft SQL
Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\Program Files\TortoiseSVN\bin
;D:\Program Files\Bazaar;C:\Program Files\Android\android-sdk\tools;D:\Program Files\
Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common
\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\
Microsoft Visual Studio\VC98\bin
I have two questions:
PATH
variable using the console (and programmatically, with a batch file)?Upvotes: 761
Views: 2533905
Reputation: 53085
I'd like to summarize and go further to improve upon what's already been said here. Here are some examples:
For all of the examples below, I'll update the PATH to include the Git bin directory to give the current command-line console or terminal access to Git for Windows's Git\bin\bash.exe
bash
executable.
The Git\bin
directory is typically located at:
C:\Program Files\Git\bin
if installed as an admin, or atC:\Users\my_username\AppData\Local\Programs\Git\bin
if installed as a local user.The path C:\Users\my_username
can be represented by the environment variable %USERPROFILE%
in Windows Command Prompt. In Windows PowerShell, use $HOME
instead of %USERPROFILE%
.
# 1. In Windows Command Prompt (cmd.exe)
PATH C:\Program Files\Git\bin;%USERPROFILE%\AppData\Local\Programs\Git\bin;%PATH%
# 2. In Windows PowerShell
$env:PATH = "C:\Program Files\Git\bin;$HOME\AppData\Local\Programs\Git\bin;$env:PATH"
# 3. In Git Bash in Git for Windows
export PATH="/c/Program Files/Git/bin:$HOME/AppData/Local/Programs/Git/bin:$PATH"
# 4. A dual-compatible script that runs in both Windows Command Prompt and
# in Bash on any OS
# - See the script at the end of this answer.
My preference:
PATH C:\Program Files\Git\bin;%USERPROFILE%\AppData\Local\Programs\Git\bin;%PATH%
Reference: @zar's answer
or:
set PATH=C:\Program Files\Git\bin;%USERPROFILE%\AppData\Local\Programs\Git\bin;%PATH%
References:
Now verify the new PATH, show that bash
is now available, and run it:
echo %PATH%
where bash
bash
$env:PATH = "C:\Program Files\Git\bin;$HOME\AppData\Local\Programs\Git\bin;$env:PATH"
Now verify the new PATH, show that bash
is now available, and run it:
echo "env:PATH = $env:PATH"
Get-Command bash | Format-List
# OR (shorter)
gcm bash | fl
bash
See help gcm
or help Get-Command
, and help fl
or help Format-List
for more information on Get-Command
and Format-List
, respectively.
Git Bash is a Windows-compatible Linux Bash shell, so use the same syntax and commands as in regular Linux Bash.
export PATH="/c/Program Files/Git/bin:$HOME/AppData/Local/Programs/Git/bin:$PATH"
Now verify the new PATH, show that bash
is now available, and run it:
echo "$PATH"
which bash
bash
If you need to fix or change your $HOME
environment variable, see my answer here: Change the location of the ~
directory in a Windows install of Git Bash
See my answer here: Single script to run in both Windows batch and Linux Bash?.
updatePath.sh.cmd
:;# DESCRIPTION: this file can be run both in Bash as well as in cmd.exe
:;# (Windows Command Prompt).
:<<BATCH
:;# =======================================================================
:;# When run in cmd.exe, this section runs
:;# =======================================================================
@echo off
PATH C:\Program Files\Git\bin;%USERPROFILE%\AppData\Local\Programs\Git\bin;%PATH%
echo "New PATH:"
echo %PATH%
echo "Done."
exit /b
BATCH
# =======================================================================
# When run in Bash, this section runs
# =======================================================================
echo "This is running in Bash."
export PATH="/c/Program Files/Git/bin:$HOME/AppData/Local/Programs/Git/bin:$PATH"
echo "New PATH: $PATH"
Run it in Linux Bash or in Git Bash in Windows:
chmod +x updatePath.sh.cmd
./updatePath.sh.cmd
# OR
bash updatePath.sh.cmd
Run it in Windows Command Prompt:
"updatePath.sh.cmd"
:: OR
.\updatePath.sh.cmd
Upvotes: 0
Reputation: 16153
After you change PATH
with the GUI, close and reopen the console window. This works because only programs started after the change will "see" the new PATH
. This is because when you change the PATH environment variable using the GUI tool, it updates the variable for future processes but not for anything currently running.
This option only affects your current shell session, not the whole system. Execute this command in the command window you have open:
set PATH=%PATH%;C:\your\path\here\
This command appends C:\your\path\here\
to the current PATH
. If your path includes spaces, you do not need to include quotation marks.
Breaking it down:
set
– A command that changes cmd's environment variables only for the current cmd session; other programs and the system are unaffected.PATH=
– This signifies that PATH
is the environment variable to be temporarily changed.%PATH%;C:\your\path\here\
– The %PATH%
part expands to the current value of PATH
, and ;C:\your\path\here\
is then concatenated to it. This becomes the new PATH
.Upvotes: 1249
Reputation: 3656
I would use PowerShell instead!
To add a directory to PATH using PowerShell, do the following:
$PATH = [Environment]::GetEnvironmentVariable("PATH")
$xampp_path = "C:\xampp\php"
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path")
To set the variable for all users, machine-wide, the last line should be like:
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
In a PowerShell script, you might want to check for the presence of your C:\xampp\php
before adding to PATH (in case it has been previously added). You can wrap it in an if
conditional.
So putting it all together:
$PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine")
$xampp_path = "C:\xampp\php"
if( $PATH -notlike "*"+$xampp_path+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
}
Better still, one could create a generic function. Just supply the directory you wish to add:
function AddTo-Path{
param(
[string]$Dir
)
if (!(Test-Path $Dir) ){
Write-warning "Supplied directory was not found!"
return
}
$PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine")
if ($PATH -notlike "*"+$Dir+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$Dir", "Machine")
}
}
You could make things better by doing some polishing. For example, using Test-Path
to confirm that your directory actually exists.
Upvotes: 80
Reputation: 121
Here my solution that keeps the REG_EXPAND_SZ type of the Path value in the registry as well as the existing "template variables" that Path contained
@echo off
:: get current Path variable from registry (user)
for /F "usebackq tokens=1,2,*" %%a in (`reg query HKCU\Environment /v Path`) do (
set "current=%%c"
)
:: prepend stuff, in this case "F:\bin;"
set prepend=F:\bin;
set "current=%prepend%%current%"
:: create a backup, just in case
reg export HKCU\Environment %TMP%\~env_backup.reg /y >nul
:: overwrite current Path in registry (user) with extended version
reg add HKCU\Environment /v Path /t REG_EXPAND_SZ /d "%current%" /f >nul
:: prepend the same stuff also for the current CMD session
set "Path=%prepend%%Path%"
Upvotes: 0
Reputation: 325
In my case it was just that I copied the path from the properties dialog box in Windows and it contained a blank character or something else in the text so it was not recognized. I pasted the path text in a plain text file and removed everything to the sides and my variable was recognized.
Upvotes: 0
Reputation: 1905
SystemPropertiesAdvanced
and click "Environment Variables", no UACrundll32 sysdm.cpl,EditEnvironmentVariables
direct, might trigger UACVia Can the environment variables tool in Windows be launched directly? on Server Fault.
You can also search for Variables
in the Start menu search.
Upvotes: 2
Reputation: 26333
Use these commands in the Bash shell on Windows to append a new location to the PATH variable
PATH=$PATH:/path/to/mydir
Or prepend this location
PATH=/path/to/mydir:$PATH
In your case, for instance, do
PATH=$PATH:C:\xampp\php
You can echo $PATH
to see the PATH variable in the shell.
Upvotes: 1
Reputation: 1012
The below solution worked perfectly.
Try the below command in your Windows terminal.
setx PATH "C:\myfolder;%PATH%"
SUCCESS: Specified value was saved.
You can refer to more on here.
Upvotes: 4
Reputation: 21
I have installed PHP that time. I extracted php-7***.zip into C:\php</i>
Back up 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 (the same way)
Modify path.txt (sure, my path length is more than 1024 characters, and Windows is running few years)
;C:\php\
Open Windows PowerShell as Administrator (e.g., Win + X).
Run command:
setx path "Here you should insert string from buffer (new path value)"
Rerun your terminal (I use "Far Manager") and check:
php -v
Upvotes: 0
Reputation: 84
Checking the above suggestions on Windows 10 LTSB, and with a glimpse on the "help" outlines (that can be viewed when typing 'command /?' on the cmd), brought me to the conclusion that the PATH command changes the system environment variable Path values only for the current session, but after reboot all the values reset to their default- just as they were prior to using the PATH command.
On the other hand using the SETX command with administrative privileges is way more powerful. It changes those values for good (or at least until the next time this command is used or until next time those values are manually GUI manipulated... ).
The best SETX syntax usage that worked for me:
SETX PATH "%PATH%;C:\path\to\where\the\command\resides"
where any equal sign '=' should be avoided, and don't you worry about spaces! There isn't any need to insert any more quotation marks for a path that contains spaces inside it - the split sign ';' does the job.
The PATH keyword that follows the SETX defines which set of values should be changed among the System Environment Variables possible values, and the %PATH% (the word PATH surrounded by the percent sign) inside the quotation marks, tells the OS to leave the existing PATH values as they are and add the following path (the one that follows the split sign ';') to the existing values.
Upvotes: 4
Reputation: 2363
Regarding point 2, I'm using a simple batch file that is populating PATH
or other environment variables for me. Therefore, there isn’t any pollution of environment variables by default. This batch file is accessible from everywhere so I can type:
mybatchfile
Output:
-- Here all environment variables are available
And:
php file.php
Upvotes: 2
Reputation: 12863
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.
Here's a full example that works on Windows 7 to set the PATH
environment variable system wide. The example detects if the software has already been added to the PATH
before attempting to change the value. There are a number of minor technical differences from the examples given above:
@echo off
set OWNPATH=%~dp0
set PLATFORM=mswin
if defined ProgramFiles(x86) set PLATFORM=win64
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set PLATFORM=win64
if exist "%OWNPATH%tex\texmf-mswin\bin\context.exe" set PLATFORM=mswin
if exist "%OWNPATH%tex\texmf-win64\bin\context.exe" set PLATFORM=win64
rem Check if the PATH was updated previously
echo %PATH% | findstr "texmf-%PLATFORM%" > nul
rem Only update the PATH if not previously updated
if ERRORLEVEL 1 (
set Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
for /F "USEBACKQ tokens=2*" %%A in (`reg query %%Key%% /v PATH`) do (
if not "%%~B" == "" (
rem Preserve the existing PATH
echo %%B > currpath.txt
rem Update the current session
set PATH=%PATH%;%OWNPATH%tex\texmf-%PLATFORM%\bin
rem Persist the PATH environment variable
setx PATH "%%B;%OWNPATH%tex\texmf-%PLATFORM%\bin" /M
)
)
)
1. Not strictly true
Upvotes: 64
Reputation: 777
As trivial as it may be, I had to restart Windows when faced with this problem.
I am running Windows 7 x64. I did a manual update to the system PATH variable. This worked okay if I ran cmd.exe from the stat menu. But if I type "cmd" in the Windows Explorer address bar, it seems to load the PATH from elsewhere, which doesn't have my manual changes.
(To avoid doubt - yes, I did close and rerun cmd a couple of times before I restarted and it didn't help.)
Upvotes: 0
Reputation: 3453
Use pathed from gtools.
It does things in an intuitive way. For example:
pathed /REMOVE "c:\my\folder"
pathed /APPEND "c:\my\folder"
It shows results without the need to spawn a new cmd!
Upvotes: 6
Reputation: 6647
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.
Upvotes: 1
Reputation:
In a command prompt you tell Cmd to use Windows Explorer's command line by prefacing it with start
.
So start Yourbatchname
.
Note you have to register as if its name is batchfile.exe
.
Programs and documents can be added to the registry so typing their name without their path in the Start - Run dialog box or shortcut enables Windows to find them.
This is a generic reg file. Copy the lines below to a new Text Document and save it as anyname.reg. Edit it with your programs or documents.
In paths, use \\
to separate folder names in key paths as regedit uses a single \
to separate its key names. All reg files start with REGEDIT4. A semicolon turns a line into a comment. The @
symbol means to assign the value to the key rather than a named value.
The file doesn't have to exist. This can be used to set Word.exe to open Winword.exe.
Typing start batchfile
will start iexplore.exe.
REGEDIT4
;The bolded name below is the name of the document or program, <filename>.<file extension>
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Batchfile.exe]
; The @ means the path to the file is assigned to the default value for the key.
; The whole path in enclosed in a quotation mark ".
@="\"C:\\Program Files\\Internet Explorer\\iexplore.exe\""
; Optional Parameters. The semicolon means don't process the line. Remove it if you want to put it in the registry
; Informs the shell that the program accepts URLs.
;"useURL"="1"
; Sets the path that a program will use as its' default directory. This is commented out.
;"Path"="C:\\Program Files\\Microsoft Office\\Office\\"
You've already been told about path in another answer. Also see doskey /?
for cmd macros (they only work when typing).
You can run startup commands for CMD. From Windows Resource Kit Technical Reference
AutoRun
HKCU\Software\Microsoft\Command Processor
Data type Range Default value
REG_SZ list of commands There is no default value for this entry.
Description
Contains commands which are executed each time you start Cmd.exe.
Upvotes: 1
Reputation: 19347
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!
Upvotes: 7
Reputation: 12247
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%
Upvotes: 96
Reputation: 2726
To override already included executables;
set PATH=C:\xampp\php;%PATH%;
Upvotes: 5
Reputation: 1327
Handy if you are already in the directory you want to add to PATH:
set PATH=%PATH%;%CD%
It works with the standard Windows cmd, but not in PowerShell.
For PowerShell, the %CD%
equivalent is [System.Environment]::CurrentDirectory
.
Upvotes: 28
Reputation: 942197
This only modifies the registry. An existing process won't use these values. A new process will do so if it is started after this change and doesn't inherit the old environment from its parent.
You didn't specify how you started the console session. The best way to ensure this is to exit the command shell and run it again. It should then inherit the updated PATH environment variable.
Upvotes: 174
Reputation: 5413
WARNING: This solution may be destructive to your PATH, and the stability of your system. As a side effect, it will merge your user and system PATH, and truncate PATH to 1024 characters. The effect of this command is irreversible. Make a backup of PATH first. See the comments for more information.
Don't blindly copy-and-paste this. Use with caution.
You can permanently add a path to PATH
with the setx
command:
setx /M path "%path%;C:\your\path\here\"
Remove the /M
flag if you want to set the user PATH
instead of the system PATH
.
Notes:
setx
command is only available in Windows 7 and later.You should run this command from an elevated command prompt.
If you only want to change it for the current session, use set.
Upvotes: 290
Reputation: 232
If you run the command cmd
, it will update all system variables for that command window.
Upvotes: 1