# how - Adding directory to PATH Environment Variable in Windows

## set path in windows cmd (11)

I am trying to add C:\xampp\php to my system PATH environment variable in Windows.

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:

1. Why did this happen? Is there something I did wrong?
2. Also, how do I add directories to my PATH variable using the console (and programmatically, with a batch file)?

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:

• The setx command is only available in Windows 7 and later.
• You should run this command from an elevated command prompt.

# Option 1

After you change PATH with the GUI, close and re-open the console window.

This works because only programs started after the change will see the new PATH.

# Option 2

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.

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= – 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.

1. I have installed PHP that time. Extracted php-7***.zip into C:\php\
2. Backup my current PATH environment variable: run cmd, and execute command: path >C:\path-backup.txt

3. Get my current path value into C:\path.txt file (same way)

4. Modify path.txt (sure, my path length is more than 1024 chars, windows is running few years)
• I have removed duplicates paths in there, like 'C:\Windows; or C:\Windows\System32; or C:\Windows\System32\Wbem; - I've got twice.
• Remove uninstalled programs paths as well. Example: C:\Program Files\NonExistSoftware;
• This way, my path string length < 1024 :)))
• at the end of path string add ;C:\php\
• Copy path value only into buffer with framed double quotes! Example: "C:\Windows;****;C:\php\" No PATH= should be there!!!
5. Open Windows PowerShell as Administrator.
6. Run command:

setx path "Here you should insert string from buffer (new path value)"

1. Re-run your terminal (I use "Far manager") and check: php -v

A better alternative to Control Panel is to use this freeware program from sourceforge called Pathenator:

https://sourceforge.net/projects/pathenator/

However, it only workers for system that has Dot.Net 4.0 or greater such as windows 7,8, or 10.

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

But for the sake of clarity i thought that sharing here the best SETX syntax usage that worked for me might help somebody one day:

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 is no need to insert any more quotation marks for a path that contains spaces inside it- the split sign ';' do 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.

HTH

If you run the command cmd, it will update all system variables for that command window.

In this age of PowerShell, I would edit PATH like so:

$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")$xampp_path = "C:\xampp\php"
if( $PATH -notlike "*"+$xampp_path+"*" ){
[Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
}


Late to the party - but handy if you are already in the directory you want to add to PATH.

set PATH=%PATH%;%CD%

edit: as per comment - works with standard windows cmd but not in powershell.

For powershell the %CD% equivalent is [System.Environment]::CurrentDirectory

This only modifies the registry. A process won't use these values until it is started after this change and doesn't inherit the environment from its parent.

You didn't specify how you started the console session. Best way to ensure this is to log out and log back in again.

What if you mistype the path using setx? The best way is simply through the windows U.I. Control Panel->All Control Panel Items->System->Advanced System Setttings->Environment Variables

Scroll down to Path and select Edit. You can also copy and paste it into your favorite editor so you can see the entire path and more easily edit it.

• command line changes will not be permanent, will be lost when the console closes.
• Path works like first comes first served.
• You may want to override other already included executables. For instance if you already have another version on your path and you want to add different version without making a permanent change on path you should put the directory at the beginning of the command.