windowsbatch-filepathcmdenvironment

Set windows environment variables with a batch file


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


Solution

  • 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?