Can a script.bat make changes to Windows PATH Environment Variable

41,626

If you wish to change/update the PATH permanently in the environment variable, you can use the SETX command e.g.

setx path "%PATH%;C:\New Folder" 

For more details information on %PATH% and other variables to access to system folder, refer to http://vlaurie.com/computers2/Articles/environment.htm

Share:
41,626
Berming
Author by

Berming

Updated on February 11, 2022

Comments

  • Berming
    Berming over 2 years

    I'm trying to write a script that when clicked will add a couple of entries to the PATH Environment variable in Windows, instead of making the changes manually. I see .bat files being used all the time on Windows for a variety of reasons, so can a .bat script help me with something like that?

    I actually need to download a zip from a location on the Internet, extract it to a specified location, then update the PATH in environment variable. Never done this before so any hints appreciated.

  • Julian Mann
    Julian Mann almost 12 years
    I tried this, and the value of %PATH% (the system variable) is prepended to "C:\New Folder". So far so good. However, it is saved as the user path variable. The path variable is then made up from the system path prepended to the user path. The result is that all the system path directories are there twice and "C:\New Folder" is stuck on the end. And the next time you try to append something, you get the system directories 3 times and so on...
  • Marcus Pope
    Marcus Pope almost 12 years
    @JulianMann You are probably looking for the /M switch which will set the system environment instead of the user environment. Running it multiple times will definitely duplicate the information because %PATH% is a variable referencing the current environment that you just modified - it appends the data and is not intended to be modified multiple times. I recommend editing the path var manually to remove duplicates <computerhope.com/issues/ch000549.htm> and then running the command only once. Be sure to execute the command with admin rights if you use the /M flag.
  • Julian Mann
    Julian Mann almost 12 years
    @Marcus thanks for the info. In my case I was giving the bat script to users (who don't have admin rights) so they could set their paths to point at my software. Its something that is fairly easy to do in bash on a unix based system. If the variable is not there, set it, otherwise append to it. The fact that the PATH is made from 2 parts on windows throws a spanner in the works.
  • SSIsyphe
    SSIsyphe over 6 years
    WARNING ! SETX is deadly dangerous, as it crops the PATH to 1024 characters (yes, we are in 2017, Windows 10). To me, using it made me lose a part of my PATH and 2 hours of my time. Not a relevant solution, except if you're looking for trouble...
  • Mofi
    Mofi almost 5 years
    NO, NO and once more NO. This line corrupts the user PATH environment variable. It uses local PATH with all folder paths of system and user PATH with all environment variables expanded to set user PATH with C:\New Folder appended. This line is really, really bad and should be never used by anyone reading this answer. It is awful. The usage of local PATH to set user or system PATH is an absolute NO GO, NEVER EVER.
  • Mofi
    Mofi almost 5 years
    For more information on how to modify persistent stored user or system PATH see for example Why are other folder paths also added to system PATH with SetX and not only the specified folder path? and Adding the current directory to Windows path permanently and How to search and replace a string in environment variable PATH? and everything referenced by these answers.