301

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:

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Shane
  • 3,600
  • 5
  • 21
  • 18
  • 2
    setx variable value - then restart Command Prompt – Andrew Aug 02 '17 at 19:34
  • This is fully documented here, for command line + powershell - http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-windows-command-line-and-registry/ – Chris Halcrow Sep 03 '20 at 00:58

5 Answers5

347

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

nhinkle
  • 37,198
  • 36
  • 140
  • 177
ChrisF
  • 41,278
  • 17
  • 101
  • 154
  • 9
    Note 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 Oct 22 '15 at 09:00
  • 13
    @MD.MohiuddinAhmed That's because there are spaces in the path. – ChrisF Oct 22 '15 at 09:15
  • 3
    this can damage your path because of a 1024 character limit! - see https://superuser.com/questions/387619/overcoming-the-1024-character-limit-with-setx – dkocich Apr 15 '20 at 20:20
  • 1
    You can check the command result by using `echo %NEWVAR%`. Current cmd will cache the Environment Variables so closing the current cmd and opening a new one will be required to see the changes done. – Beytan Kurt Jul 03 '20 at 16:16
  • 2
    And for Windows Terminal, you currently (nov 2020) need to close all instances and restart. – Sire Nov 19 '20 at 11:52
48

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.

music2myear
  • 40,472
  • 44
  • 86
  • 127
Minh Chau
  • 491
  • 4
  • 2
11

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
  • 211
  • 2
  • 5
6

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.

1

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
  • 91
  • 1
  • 3