- How to Add to Windows PATH Environment Variable
- Add Directories to PATH Variable
- How to export and import environment variables in windows?
- 8 Answers 8
- Adding a directory to the PATH environment variable in Windows
- 18 Answers 18
- Option 1
- Option 2
- Safer SETX
- Warnings
- Usage instructions
- Append to User PATH
- Append to System PATH
- Alternatives
How to Add to Windows PATH Environment Variable
Works for Windows 10 or 7
If you’re a coder or programmer, you probably spend a decent amount of time using the command prompt to execute programs or compile code. In order to complete those tasks, you most likely have to use a command from a library or software package installed (like Python) on your system.
By default, most of these programs will add their own custom shortcuts to the Windows environment variables. The most used environment variable in Windows is probably the PATH variable. It basically allows you to run any executables that are located inside the paths specified in the variable at the command prompt without having to give the full path to the executable.
In this article, I’ll show you how you can add more paths to the Windows PATH variable in case you want to run executables from your own custom directories. It’s worth noting that the procedure below is for Windows 10, but it’s almost exactly the same for Windows 7 also.
Add Directories to PATH Variable
To get started, right-click on the Computer or This PC icon on the desktop and select Properties. If you don’t have that icon on your desktop already, you can add any missing desktop icons easily.
On the System dialog page, you’ll see an Advanced system settings link on the left-hand side.
This will bring up the System Properties dialog, which should already be open to the Advanced tab. Go ahead and click on the Environment Variables button at the very bottom.
On the Environment Variables dialog, you’ll see two sets of variables: one for user variables and the other for system variables. Both lists have the PATH variable, so you have to decide which one to edit.
If you only need the commands for your own user account, then edit the user variable. If you need it to work across the computer system regardless of which user is logged in, then edit the system variable. Click on Path and then click on Edit.
On the Edit environment variable dialog, you’ll see a list of all the paths that are currently in the PATH variable. As you can see, Node.js and Git already added their paths so that I can run Git commands and Node.js commands from anywhere while in the command prompt.
To add a new path, simply click on New and it’ll add a new line to the bottom of the list. If you know the path, simply type it in or copy and paste it. If you prefer, you can also click Browse and then navigate to the desired path.
To edit any path, simply select it and then click on the Edit button. You can also delete paths using the Delete button. Note that you can also move items up and down on the list. When you type a command at the command prompt, Windows has to search through each directory stored in the PATH variable to see if that executable exists or not. If you want your executable to be found faster, just move that path up to the top of the list.
This can also come in handy if you have multiple versions of the same command in different paths and need to have one run instead of the other. The one that shows up higher in the list will be run when you type in the command.
Lastly, if you click on Edit text, it will load a dialog where you can edit the Path variable using the old interface where all the paths are listed in one text box.
That’s all there is to it! If you want to learn more about environment variables, make sure to check out my post on how to create your own custom environment variables. Enjoy!
Founder of Help Desk Geek and managing editor. He began blogging in 2007 and quit his job in 2010 to blog full-time. He has over 15 years of industry experience in IT and holds several technical certifications. Read Aseem’s Full Bio
How to export and import environment variables in windows?
I found it is hard to keep my environment variables sync on different machines. I just want to export the settings from one computer and import to other ones.
I think it should be possible, but don’t know how to do it. Can anyone help me? Thanks.
8 Answers 8
You can use RegEdit to export the following two keys:
The first set are system/global environment variables; the second set are user-level variables. Edit as needed and then import the .reg files on the new machine.
I would use the SET command from the command prompt to export all the variables, rather than just PATH as recommended above.
To import the variablies, one can use a simple loop:
To export user variables, open a command prompt and use regedit with /e
Combine @vincsilver and @jdigital’s answers with some modifications,
- export .reg to current directory
- add date mark
Output would like:
10,4% – Rakesh N Oct 5 ’17 at 10:54
You can get access to the environment variables in either the command line or in the registry.
Command Line
If you want a specific environment variable, then just type the name of it (e.g. PATH ), followed by a > , and the filename to write to. The following will dump the PATH environment variable to a file named path.txt.
Registry Method
The Windows Registry holds all the environment variables, in different places depending on which set you are after. You can use the registry Import/Export commands to shift them into the other PC.
Adding a directory to the PATH environment variable in Windows
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:
it doesn’t show the new C:\xampp\php directory:
I have two questions:
- Why did this happen? Is there something I did wrong?
- Also, how do I add directories to my PATH variable using the console (and programmatically, with a batch file)?
18 Answers 18
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.
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:
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 .
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:
Remove the /M flag if you want to set the user PATH instead of the system PATH .
- The 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.
You don’t need any set or setx command. Simply open the terminal and type:
This shows the current value of PATH variable. Now you want to add directory to it? Simply type:
If for any reason you want to clear the PATH variable (no paths at all or delete all paths in it), type:
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:
I would use PowerShell instead!
To add a directory to PATH using PowerShell, do the following:
To set the variable for all users, machine-wide, the last line should be like:
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:
Better still, one could create a generic function. Just supply the directory you wish to add:
You could make things better by doing some polishing. For example, using Test-Path to confirm that your directory actually exists.
Safer SETX
- SETX by default will update your user path.
- SETX . /M will update your system path.
- %PATH% contains the system path with the user path appended
Warnings
- Backup your PATH — SETX will truncate your junk longer than 1024 characters
- Don’t call SETX %PATH%;xxx — adds the system path into the user path
- Don’t call SETX %PATH%;xxx /M — adds the user path into the system path
- Excessive batch file use can cause blindness 1
The ss64 SETX page has some very good examples. Importantly it points to where the registry keys are for SETX vs SETX /M
Usage instructions
Append to User PATH
Append to System 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.)
Alternatives
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
Handy if you are already in the directory you want to add to PATH:
It works with the standard Windows cmd, but not in PowerShell.
For PowerShell, the %CD% equivalent is [System.Environment]::CurrentDirectory .
- Command line changes will not be permanent and will be lost when the console closes.
- The 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.
To override already included executables;
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!
It does things in an intuitive way. For example:
It shows results without the need to spawn a new cmd!
Regarding point 2 I’m using a simple batch file that is populating PATH or other environment variables for me. Therefore, there is no pollution of environment variables by default. This batch file is accessible from everywhere so I can type:
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:
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.