Ruben
Ruben

Reputation: 9186

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

Upvotes: 88

Views: 329153

Answers (4)

Ruben
Ruben

Reputation: 9186

@ECHO OFF

:: %HOMEDRIVE% = C:
:: %HOMEPATH% = \Users\Ruben
:: %system32% ??
:: No spaces in paths
:: Program Files > ProgramFiles
:: cls = clear screen
:: CMD reads the system environment variables when it starts. To re-read those variables you need to restart CMD
:: Use console 2 http://sourceforge.net/projects/console/


:: Assign all Path variables
SET PHP="%HOMEDRIVE%\wamp\bin\php\php5.4.16"
SET SYSTEM32="%HOMEDRIVE%\Windows\System32"
SET ANT="%HOMEDRIVE%%HOMEPATH%\Downloads\apache-ant-1.9.0-bin\apache-ant-1.9.0\bin"
SET GRADLE="%HOMEDRIVE%\tools\gradle-1.6\bin;"
SET ADT="%HOMEDRIVE%\tools\adt-bundle-windows-x86-20130219\eclipse\jre\bin"
SET ADTTOOLS="%HOMEDRIVE%\tools\adt-bundle-windows-x86-20130219\sdk\tools"
SET ADTP="%HOMEDRIVE%\tools\adt-bundle-windows-x86-20130219\sdk\platform-tools"
SET YII="%HOMEDRIVE%\wamp\www\yii\framework"
SET NODEJS="%HOMEDRIVE%\ProgramFiles\nodejs"
SET CURL="%HOMEDRIVE%\tools\curl_734_0_ssl"
SET COMPOSER="%HOMEDRIVE%\ProgramData\ComposerSetup\bin"
SET GIT="%HOMEDRIVE%\Program Files\Git\cmd"

:: Set Path variable
setx PATH "%PHP%;%SYSTEM32%;%NODEJS%;%COMPOSER%;%YII%;%GIT%" /m

:: Set Java variable
setx JAVA_HOME "%HOMEDRIVE%\ProgramFiles\Java\jdk1.7.0_21" /m

PAUSE

Upvotes: 121

CrisSorryIfImWrong
CrisSorryIfImWrong

Reputation: 11

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

Upvotes: 1

phnghue
phnghue

Reputation: 1686

*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

Upvotes: 0

xjcl
xjcl

Reputation: 15299

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.

Upvotes: 8

Related Questions