Setting and getting Windows environment variables from the command prompt?

226

52

I want to set an environment variable from the command prompt and then be able to access it globally (for instance, I should see it by going to System -> Environment Variables).

When I use the set command, it isn't accessible in a new cmd session.

set NEWVAR=SOMETHING
echo %NEWVAR%

Related questions:

Shane

Posted 2009-12-06T21:52:22.253

Reputation: 2 810

1setx variable value - then restart Command Prompt – Andrew – 2017-08-02T19:34:35.953

Answers

260

To make the environment variable accessible globally you need to set it in the registry. As you've realised by just using:

set NEWVAR=SOMETHING

you are just setting it in the current process space.

According to this page you can use the setx command:

setx NEWVAR SOMETHING

setx is built into Windows 7, but for older versions may only be available if you install the Windows Resource Kit

ChrisF

Posted 2009-12-06T21:52:22.253

Reputation: 39 650

4Note that you need to specify quota, for example : setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_45" will work. But setx JAVA_HOME C:\Program Files\Java\jdk1.7.0_45 will give you syntax errors – MD. Mohiuddin Ahmed – 2015-10-22T09:00:39.393

8@MD.MohiuddinAhmed That's because there are spaces in the path. – ChrisF – 2015-10-22T09:15:14.703

36

We can also use "setx var variable /M" to set the var to system environment variable level instead of user level.

Note: This command should be run as administrator.

Minh Chau

Posted 2009-12-06T21:52:22.253

Reputation: 371

11

setx program "C:\Program Files" /M

/M for set system environment variable level instead of user level like @Minh Chau answer

Test enter image description here

RESTART command line (if you don't restart command line, environment variable will not work)

enter image description here

Phan Van Linh

Posted 2009-12-06T21:52:22.253

Reputation: 211

This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill – 2017-02-09T10:23:00.443

8@DavidPostill my answer have a new is we need restart command line. I think it is important – Phan Van Linh – 2017-02-09T13:14:57.130

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.

Himanshu Singh

Posted 2009-12-06T21:52:22.253

Reputation: 21

Please read the question again carefully. Your answer does not answer the original question. – DavidPostill – 2017-03-09T13:39:43.127

It's close, though. Using REG ADD may accomplish this? – TOOGAM – 2017-03-10T09:05:44.153

2

You can use setx env var [/M] as mentioned above. If it doesn't take effect you can use refreshenv to refresh environment variables. You don't have to restart your computer, explorer.exe or your command prompt to do that.

Edit: apparantly refreshenv doesn't come naturally with Windows, so here's the source: https://pastebin.com/1fJqA0pT
Save as RefreshEnv.cmd and place it in a folder that's included in your PATH environment variables

DFSFOT

Posted 2009-12-06T21:52:22.253

Reputation: 21

1+1 for mentioning 'refreshenv' - I'd never come across that before! – Francis Norton – 2019-03-01T15:27:51.123

0

I want to add that if you are using the /s parameter with setx in order to set environment variables on a remote computer, the "Remote Registry" service needs to be running on the target machine or else you will receive a "ERROR: The specified operation could not be completed."

(I have asked Microsoft to update their TechNet article on setx to include this information.)

Tim Bailen

Posted 2009-12-06T21:52:22.253

Reputation: 61