I want to modify the Windows PATH variable using setx
. The following works at least 50% of the time on Windows 8:
setx PATH %PATH%;C:\Python27\;C:\Python27\Scripts\
If it gives the error "the default argument can only be used 2 times", then the following works some of the time:
setx PATH "%PATH%;C:\Python27\;C:\Python27\Scripts\"
The difference is that we wrapped the second argument in quotes. I believe the quotes are necessary when %PATH%
expands to include spaces.
However, I have encountered some weird problems on Windows 7. On one particular Windows 7 machine, I had this problem:
echo %PATH%
It prints:
C:\Foo\;C:\Bar\;[...lots of stuff...]C:\Baz\
Then I do this:
setx PATH "%PATH%;C:\Quux\"
Then it says "Error: Truncated at 1,024 characters." Now let's check what PATH contains:
echo %PATH%
It prints:
C:\Foo\;C:\Foo\;C:\Bar\;C:\Bar\;[...lots of stuff, now duplicated...]C:\B
...and it is cut off at 1,024 characters. It ran over because of the duplicates. Also interesting: The value of PATH changes despite the fact that setx
raised an error and did not say "Success".
I was able to repeat this strange behavior several times (luckily I had saved the original contents of PATH).
At the moment, the only surefire way I know to append to the PATH is the following:
echo
the PATH.Copy the contents of PATH into a text file and manually add
;C:\Python27\;C:\Python27\Scripts\
to the end of the PATH.Copy the whole thing out of the text file.
setx PATH "<paste the string here>"
That process works every single time on both Windows 7 and Windows 8.
I should really be able to do this in one command. What am I doing wrong?
Thank you.
This works perfectly:
The 1st command gets the USER environment variable 'PATH', into 'my_user_path' variable The 2nd line prepends the 'C:\Python27;C:\Python27\Scripts;' to the USER environment variable 'PATH'
I was facing the same problems and found a easy solution now.
Using pathman.
Adds for example %M2% to the system path. Nothing more and nothing less. No more problems getting a mixture of user PATH and system PATH. No more hardly trying to get the correct values from registry...
Tried at Windows 10
Steps: 1. Open a command prompt with administrator's rights.
Steps: 2. Run the command: setx /M PATH "path\to;%PATH%"
[Note: Be sure to alter the command so that path\to reflects the folder path from your root.]
Example : setx /M PATH "C:\Program Files;%PATH%"
Without admin rights the only way that worked for me is a bat file that contains the following code:
The code is the combination of the answers https://stackoverflow.com/a/45566845/4717152 and https://stackoverflow.com/a/10292113/4717152
If someone want to run it in PowerShell it works like below,
Run Powershell as Administrator
Then
To verify, open another Powershell and view PATH as below,
I was having such trouble managing my computer labs when the %PATH% environment variable approached 1024 characters that I wrote a Powershell script to fix it.
You can download the code here: https://gallery.technet.microsoft.com/scriptcenter/Edit-and-shorten-PATH-37ef3189
You can also use it as a simple way to safely add, remove and parse PATH entries. Enjoy.