Setting a system environment variable from a Windows batch file?
Asked Answered
B

7

228

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.

Barns answered 27/9, 2010 at 12:7 Comment(0)
C
197

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.

Column answered 27/9, 2010 at 14:49 Comment(5)
Windows 7 (maybe Vista already) has setx standard, with a minor change? there is no -i flag and just running setx /? displays all help + examplesRimma
Remember 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 case where 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.exeFlannery
@Rimma - The Win7 version also officially changed the flag introducer from - to the forward slash /. However, it looks like the - version still works.Aleda
There seems to be a 1024 length limit to the setx variable contentDunlin
The setx provided by XP SP2 Support Tools requires forward slash / and the dash - does not work. setx variable value /mIsherwood
M
169

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" (or -m, prior to Windows 7).

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

Metalworking answered 25/6, 2012 at 13:30 Comment(1)
also 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"Kristankriste
C
26

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
Chas answered 13/1, 2016 at 21:36 Comment(0)
I
3

SetX is the command that you'll need in most of the cases.Though its possible to use REG or REGEDIT

Using registry editing commands you can avoid some of the restrictions of the SetX command - different data types, variables containing = in their name and so on.

@echo off

:: requires admin elevated permissions
::setting system variable
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyVar /D MyVal
::expandable variable
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /T REG_EXPAND_SZ /v MyVar /D MyVal


:: does not require admin permissions
::setting user variable
REG ADD "HKEY_CURRENT_USER\Environment" /v =C: /D "C:\\test"

REG is the pure registry client but its possible also to import the data with REGEDIT though it allows using only hard coded values (or generation of a temp files). The example here is a hybrid file that contains both batch code and registry data (should be saved as .bat - mind that in batch ; are ignored as delimiters while they are used as comments in .reg files):

REGEDIT4

; @ECHO OFF
; CLS
; REGEDIT.EXE /S "%~f0"
; EXIT

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"SystemVariable"="GlobalValue"

[HKEY_CURRENT_USER\Environment]
"UserVariable"="SomeValue"
Indeterminacy answered 9/11, 2020 at 22:52 Comment(0)
T
2

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).

Turntable answered 8/11, 2013 at 16:1 Comment(0)
L
2

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.

Lignocellulose answered 9/3, 2017 at 5:4 Comment(0)
K
1

Just in case you would need to delete a variable, you could use SETENV from Vincent Fatica available at http://barnyard.syr.edu/~vefatica. Not exactly recent ('98) but still working on Windows 7 x64.

Katzenjammer answered 2/5, 2014 at 21:27 Comment(2)
setx allows you to delete also by setting the value to blankCrook
@SteveLloyd and others who find this: I found that setx only let me do that when I ran it in cmd.exe and not when run in powershell fyiToneytong

© 2022 - 2024 — McMap. All rights reserved.