Setting a system environment variable from a Windows batch file?
Solution 1
The XP Support Tools (which can be installed from your XP CD) come with a program called setx.exe
:
C:\Program Files\Support Tools>setx /?
SETX: This program is used to set values in the environment
of the machine or currently logged on user using one of three modes.
1) Command Line Mode: setx variable value [-m]
Optional Switches:
-m Set value in the Machine environment. Default is User.
...
For more information and example use: SETX -i
I think Windows 7 actually comes with setx
as part of a standard install.
Solution 2
Simple example for how to set JAVA_HOME with setx.exe
in command line:
setx JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
This will set environment variable "JAVA_HOME" for current user. If you want to set a variable for all users, you have to use option "-m". Here is an example:
setx -m JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
Note: you have to execute this command as Administrator.
Note: Make sure to run the command setx from an command-line Admin window
Solution 3
If you set a variable via SETX, you cannot use this variable or its changes immediately. You have to restart the processes that want to use it.
Use the following sequence to directly set it in the setting process too (works for me perfectly in scripts that do some init stuff after setting global variables):
SET XYZ=test
SETX XYZ test
Solution 4
System variables can be set through CMD and registry For ex. reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
All the commonly used CMD codes and system variables are given here: Set Windows system environment variables using CMD.
Open CMD and type Set
You will get all the values of system variable.
Type set java to know the path details of java installed on your window OS.
Solution 5
For XP, I used a (free/donateware) tool called "RAPIDEE" (Rapid Environment Editor), but SETX is definitely sufficient for Win 7 (I did not know about this before).
Related videos on Youtube
Santhosh
Am currently working in Philips. Like most things computing. Especially Algorithms, Mathematics, NLP, Operating Systems. I use .NET framework for daily programming.. Looking for that divine touch yet to cross the boundary...
Updated on June 27, 2021Comments
-
Santhosh almost 3 years
Is it possible to set a environment variable at the system level from a command prompt in Windows 7 (or even XP for that matter). I am running from an elevated command prompt.
When I use the
set
command (set name=value
), the environment variable seems to be only valid for the session of the command prompt. -
stijn almost 11 yearsWindows 7 (maybe Vista already) has setx standard, with a minor change? there is no
-i
flag and just runningsetx /?
displays all help + examples -
samael almost 11 yearsalso note that some tools don't like spaces in JAVA_HOME so it's worth using
Progra~1
like so:setx /M JAVA_HOME "C:\Progra~1\Java\jdk1.7.0_09"
-
aolszowka over 10 yearsRemember you can always check to see if you have this program on your path by typing
where
and then the name of the program (so in this casewhere setx.exe
in testing this on a Server 2008 and Server 2008SP2 box I found that in both cases it existed at%windir%\System32\setx.exe
-
T.E.D. over 10 years@stijn - The Win7 version also officially changed the flag introducer from
-
to the forward slash/
. However, it looks like the-
version still works. -
zhy2002 over 10 yearsThere seems to be a 1024 length limit to the setx variable content
-
Steve Lloyd about 9 yearssetx allows you to delete also by setting the value to blank
-
Philip Rego almost 7 yearsWhere has this been all my life.
-
Keith over 6 yearsThe setx provided by XP SP2 Support Tools requires forward slash
/
and the dash-
does not work.setx variable value /m
-
Salvador Valencia about 6 yearsThank you, this was driving me crazy! CMD.EXE needs desperately an upgrade to behave like a normal console...!
-
Danijel almost 6 yearsI don't get this: where do I run those 2 lines?
-
Brian H. about 5 yearsYou can also execute the command
refreshenv
after using setx to make use of the new variables. -
chamberlainpi about 4 years@BrianHVB
refreshenv
not recognized as a command. Are you doing this incmd
orpowershell
? (EDIT: tested in both actually, doesn't work in either consoles) -
Brian H. about 4 years@bigp oops, you're right. It's a command that is added when chocolatey is installed. I have chocolatey on all of my systems and didn't realize until I looked it up.
-
chamberlainpi about 4 yearsDid a bit of research, it requires Chocolatey.