Set windows environment variables with a batch file
Asked Answered
M

3

83

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

Melanism answered 6/2, 2014 at 15:3 Comment(4)
I 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...Fall
Well 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?Melanism
nevermind, just use this tool: rapidee.com/en/aboutMelanism
Possible duplicate of Setting a system environment variable from a Windows batch file?Enow
M
118
@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
Melanism answered 6/2, 2014 at 15:3 Comment(4)
Looks great! My collection of paths strewn across Windows variables and Emacs variables needed some cleaning up, and I will try this. Very educational for me, thanks!Columbic
Thank you, that is exactly what I was looking for. I wanted to ask you guys, why has JAVA_HOME setx instead of SET like it is in the previous Path variables ? Thank yoMisrule
@Misrule SET initiates a variable in the console, setx actually sets the variables in the windows environment variables. You could just set the string in setx PATH , but that would make it unreadable, and the string can get so long you'll get an error.Melanism
Small, but very important note here: The quotation marks around the value are optional. They actually are taken into the env variable! I found about because i was using an env variable for a path inside log4j, and appended a subfolder to it. Which caused a crash because the result was something like: "C:/mylogfolder"/appended_subfolder - this crashes log4j (understandably).Columella
T
6

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.

Trident answered 7/6, 2022 at 9:35 Comment(0)
D
0

*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
Dish answered 12/2, 2023 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.