I was looking for a way to set the environment path variable with a .cmd file.
When the path variable was getting too long, I got some errors.
Just add the needed variables to 'Set Path variable' below
Check the current value of your path variable and add to the script
Run the script as administrator!
Open a new console window and it should work e.g. php -v
- 21I am afraid I don't understand the purpose of this topic. The "question" is not a question at all. It doesn't describe the problem nor the desired result, but include a "part of an answer" instead! On the other hand, the answer below seems to have no relation with the "question", so its usefulness its limited. The fact that the same person posted both parts is no excuse to bypass the question/answer format for topics on this forum...Aacini– Aacini2014-02-06 16:51:38 +00:00Commented Feb 6, 2014 at 16:51
- 4Well it's just something I was looking for, and I could not find a decent answer, took me a long time to make this, so I added it as a community wiki so others can use it. Should I remove this?Ruben– Ruben2014-02-07 07:46:19 +00:00Commented Feb 7, 2014 at 7:46
- 2nevermind, just use this tool: rapidee.com/en/aboutRuben– Ruben2014-04-30 06:49:10 +00:00Commented Apr 30, 2014 at 6:49
- Possible duplicate of Setting a system environment variable from a Windows batch file?jww– jww2015-06-10 06:27:29 +00:00Commented Jun 10, 2015 at 6:27
4 Answers
This batch file must be run as administrator to update system environment variable Path and set/update system environment variable JAVA_HOME.
@ECHO OFF :: %ALLUSERSPROFILE% = C:\ProgramData :: %ProgramFiles% = C:\Program Files :: %USERPROFILE% = C:\Users\Ruben :: %SystemDrive% = C: :: %SystemRoot% = C:\WINDOWS :: No spaces in paths :: CMD inherits the environment variables when it is start from Windows shell. :: CMD must be restarted to inherit the updated system environment variables. :: Use console 2 https://sourceforge.net/projects/console/ REM Assign all Path variables SET "PHP=%SystemDrive%\wamp\bin\php\php5.4.16" SET "SYSTEM32=%SystemRoot%\System32" SET "ANT=%USERPROFILE%\Downloads\apache-ant-1.9.0-bin\apache-ant-1.9.0\bin" SET "GRADLE=%SystemDrive%\tools\gradle-1.6\bin" SET "ADT=%SystemDrive%\tools\adt-bundle-windows-x86-20130219\eclipse\jre\bin" SET "ADTTOOLS=%SystemDrive%\tools\adt-bundle-windows-x86-20130219\sdk\tools" SET "ADTP=%SystemDrive%\tools\adt-bundle-windows-x86-20130219\sdk\platform-tools" SET "YII=%SystemDrive%\wamp\www\yii\framework" SET "NODEJS=%ProgramFiles%\nodejs" SET "CURL=%SystemDrive%\tools\curl_734_0_ssl" SET "COMPOSER=%ALLUSERSPROFILE%\ComposerSetup\bin" SET "GIT=%ProgramFiles%\Git\cmd" SET "JAVA_HOME=%ProgramFiles%\Java\jdk1.7.0_21" REM Set JAVA_HOME variable as system environment variable. setlocal EnableExtensions DisableDelayedExpansion set "JaveHomeReg=" for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v JAVA_HOME 2^>nul') do ( if /I "%%G" == "JAVA_HOME" ( set "JaveHomeReg=%%I" if defined JaveHomeReg goto SetJavaHome ) ) :SetJavaHome setlocal EnableDelayedExpansion if /I "!JaveHomeReg!" == "!JAVA_HOME!" endlocal & goto SetSystemPath endlocal if exist %SystemRoot%\System32\setx.exe ( %SystemRoot%\System32\setx.exe JAVA_HOME "%JAVA_HOME%" /M >nul ) else ( %SystemRoot%\System32\reg.exe ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v JAVA_HOME /t REG_SZ /d "%JAVA_HOME%" >nul ) REM Update the system Path variable. :SetSystemPath set "SystemPath=" for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul') do ( if /I "%%G" == "Path" ( set "SystemPath=%%I" if defined SystemPath goto ProcessPath ) ) :ProcessPath setlocal EnableDelayedExpansion rem Is the system environment variable PATH not present or has an empty value? if not defined SystemPath set "PathToSet=%%SystemRoot%%\System32;!PHP!;!NODEJS!;!COMPOSER!;!YII!;!GIT!" & goto UpdatePath endlocal rem It is not possible to add folder paths to the system environment rem variable PATH if this variable as stored in the registry contains rem already a folder path with an equal sign or enclosed in double quotes. set "PathCheck=%SystemPath:"=%" for /F "eol=| tokens=1 delims==" %%I in ("%PathCheck%") do if not "%%I" == "%PathCheck%" set "ErrorChar==" & goto ErrorPath setlocal EnableDelayedExpansion if "!PathCheck!" == "!SystemPath!" goto CheckPath endlocal set "ErrorChar="" goto ErrorPath rem Determine if the system environment variable PATH ends already rem with a semicolon in which case no additional semicolon must rem be added left to the folder paths to add. :CheckPath if "!SystemPath:~-1!" == ";" (set "Separator=") else set "Separator=;" set "PathCheck=!SystemPath!%Separator%" set "PathToSet=!PathCheck!" rem For each folder path to add check if the folder path without or with a rem backslash at the end with a semicolon appended for an entire folder path rem check is already in the system PATH value. This code does not work on rem path to add contains an equal sign which is fortunately very rare. if not "!PathCheck:%PHP%;=!" == "!PathCheck!" goto CheckNodeJS if not "!PathCheck:%PHP%\;=!" == "!PathCheck!" goto CheckNodeJS set "PathToSet=!PathToSet!!PHP!;" :CheckNodeJS if not "!PathCheck:%NODEJS%;=!" == "!PathCheck!" goto CheckComposer if not "!PathCheck:%NODEJS%\;=!" == "!PathCheck!" goto CheckComposer set "PathToSet=!PathToSet!!NODEJS!;" :CheckComposer if not "!PathCheck:%COMPOSER%;=!" == "!PathCheck!" goto CheckYII if not "!PathCheck:%COMPOSER%\;=!" == "!PathCheck!" goto CheckYII set "PathToSet=!PathToSet!!COMPOSER!;" :CheckYII if not "!PathCheck:%YII%;=!" == "!PathCheck!" goto CheckGit if not "!PathCheck:%YII%\;=!" == "!PathCheck!" goto CheckGit set "PathToSet=!PathToSet!!YII!;" :CheckGit if not "!PathCheck:%GIT%;=!" == "!PathCheck!" goto CheckUpdate if not "!PathCheck:%GIT%\;=!" == "!PathCheck!" goto CheckUpdate set "PathToSet=!PathToSet!!GIT!;" :CheckUpdate rem Do nothing if no folder path to add to system environment variable PATH. rem Remove otherwise the semicolon from the end of the PATH string to set. if "!PathToSet!" == "!PathCheck!" goto EndBatch set "PathToSet=!PathToSet:~0,-1!" :UpdatePath set "UseSetx=1" if not "!PathToSet:~1024,1!" == "" set "UseSetx=" if not exist %SystemRoot%\System32\setx.exe set "UseSetx=" if defined UseSetx ( %SystemRoot%\System32\setx.exe Path "!PathToSet!" /M >nul ) else ( set "ValueType=REG_EXPAND_SZ" if "!PathToSet:%%=!" == "!PathToSet!" set "ValueType=REG_SZ" %SystemRoot%\System32\reg.exe ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v Path /t !ValueType! /d "!PathToSet!" >nul ) goto EndBatch :ErrorPath echo( echo ERROR: Folder paths to add to system environment variable PATH are: echo( setlocal EnableDelayedExpansion echo !PHP! echo !NODEJS! echo !COMPOSER! echo !YII! echo !GIT! echo( echo The system environment variable PATH contains the character '%ErrorChar%'. echo This procedure for updating the system environment variable PATH echo does not support updating the variable PATH with that character. echo( echo The folder paths must be manually added to the system environment variable PATH. echo Please click on Windows Start button, type on keyboard the word environment, echo and click on suggested item "Edit the system environment variables". echo Add or edit the environment variable PATH in the lower list of system echo environment variables and append the folder paths as displayed here echo which are not already in the list of folder paths. echo( :EndBatch endlocal endlocal PAUSE The code for the update of the system environment variable Path is derived from:
What is the best practice for adding a directory to the PATH environment variable on Windows?
3 Comments
I come at this question with a Linux perspective. Usually setting an environment variable in Linux ($myVar=1) only sets it for the current process but not any child process that it spawns.
To allow any child process to read a variable you need to export envVar=2. In Windows the set command already does this for you. This is generally what you want.
The setx command sets a variable permanently for the current user, but strangely enough this is not reflected in the current process, you will need to open another cmd.exe window for it to take effect.
C:\> set foobar=1 C:\> powershell "echo ${env:foobar}" 1 C:\> setx barfoo 2 SUCCESS: Specified value was saved. C:\> powershell "echo ${env:barfoo}" # not present C:\> Also notice the strictly necessary syntax difference between set and setx.
1 Comment
PATH which is the most important environment variable. Difference between Dynamic variables and Environment variables in CMD should be read as well to understand how most predefined Windows environment variables are defined by the Windows shell.For creating a new variable with its value I simply created a .reg file that made a new registry item on the corresponding field which took care of that, for adding a value to the pre-existing PATH field out of the many values this batch file should suffice but use at your own discretion, should read the previous content and append to it.
@echo off :: Read the current Path value for /f "tokens=2*" %%A in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "CURRENT_PATH=%%B" :: Append multiple new paths set "CURRENT_PATH=%CURRENT_PATH%;BOOM" :: To add a new line just do (((set "CURRENT_PATH=%CURRENT_PATH%;BOOM"))) :: up there, Replace BOOM with desired location and remove the 3 parenthesis' :: Add double percentage if one is present i.e. "home" = "%%home%%" :: Write the updated Path back to the registry reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%CURRENT_PATH%" /f :: Confirm the update echo Path variable updated with: echo - All done now! pause 1 Comment
*Please be aware that using setx and reg add will overwrite the exist value of variable. Make sure to backup the data if you test on important system variable
You need to read the variable data first and concat with your new value to append the data. Example below will append python path to Path variable in Current User Environment:
@echo off set sMyPath=E:\python-3.8.8;E:\python-3.8.8\Scripts for /f "tokens=3" %%a in ('reg query "HKCU\Environment" /v Path') do set OLD_DATA=%%a reg add "HKCU\Environment" /v Path /d "%OLD_DATA%;%sMyPath%;" /f pause exit 1 Comment
PATH like on one folder path in existing user environment variable PATH contains a space character and a lot more reasons. I recommend reading: What is the best practice for adding a directory to the PATH environment variable on Windows?