Setting a system environment variable from a Windows batch file?

371,750

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

Share:
371,750

Related videos on Youtube

Santhosh
Author by

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, 2021

Comments

  • Santhosh
    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
    stijn almost 11 years
    Windows 7 (maybe Vista already) has setx standard, with a minor change? there is no -i flag and just running setx /? displays all help + examples
  • samael
    samael almost 11 years
    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"
  • aolszowka
    aolszowka over 10 years
    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.exe
  • T.E.D.
    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
    zhy2002 over 10 years
    There seems to be a 1024 length limit to the setx variable content
  • Steve Lloyd
    Steve Lloyd about 9 years
    setx allows you to delete also by setting the value to blank
  • Philip Rego
    Philip Rego almost 7 years
    Where has this been all my life.
  • Keith
    Keith over 6 years
    The setx provided by XP SP2 Support Tools requires forward slash / and the dash - does not work. setx variable value /m
  • Salvador Valencia
    Salvador Valencia about 6 years
    Thank you, this was driving me crazy! CMD.EXE needs desperately an upgrade to behave like a normal console...!
  • Danijel
    Danijel almost 6 years
    I don't get this: where do I run those 2 lines?
  • Brian H.
    Brian H. about 5 years
    You can also execute the command refreshenv after using setx to make use of the new variables.
  • chamberlainpi
    chamberlainpi about 4 years
    @BrianHVB refreshenv not recognized as a command. Are you doing this in cmd or powershell? (EDIT: tested in both actually, doesn't work in either consoles)
  • Brian H.
    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
    chamberlainpi about 4 years
    Did a bit of research, it requires Chocolatey.