203

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:

  • 1
    setx variable value - then restart Command Prompt – Andrew Aug 2 '17 at 19:34
233

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

  • 4
    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 9:00
  • 5
    @MD.MohiuddinAhmed That's because there are spaces in the path. – ChrisF Oct 22 '15 at 9:15
  • Oops! I didn't notice that! ... Still good point to note :) Thanks @ChrisF ! – MD. Mohiuddin Ahmed Oct 22 '15 at 15:14
  • 3
    it's a quote, not quota – phuclv Mar 24 '17 at 6:04
33

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.

9
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

  • This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill Feb 9 '17 at 10:23
  • 7
    @DavidPostill my answer have a new is we need restart command line. I think it is important – Phan Van Linh Feb 9 '17 at 13:14
1

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.

  • Please read the question again carefully. Your answer does not answer the original question. – DavidPostill Mar 9 '17 at 13:39
  • It's close, though. Using REG ADD may accomplish this? – TOOGAM Mar 10 '17 at 9:05
1

Old thread I stumbled upon,

As mentioned in @ChrisF answer you can set variable using set or setx command.

And for fetching the values you can use

>set variable_name

And you can dump all the environmental variable with values by using set command like this without giving any parameter.

>set

And for unsetting the variable we can use

>set variable_name=

By giving no value we can remove the variable from current space.

  • 1
    That should be set variable_name= (beware of spaces after variable names). – DavidPostill Feb 22 '17 at 9:57
1

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.

  • +1 for mentioning 'refreshenv' - I'd never come across that before! – Francis Norton Mar 1 at 15:27
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.)

0

Using CMD to Set Windows system environment variables : -

enter image description here

To set the variable using CMD, you have to type different codes. I have mentioned all the possible CMD codes to Modify or Add Windows system environment variables by CMD. Check out the full list of codes below. Open CMD first and begin typing the codes mentioned below. The codes are in BOLD letter.

  1. To view all available environment variables type SET and press Enter
  2. To see an environment variable value or path: Type set variable name for example, set java will show you the path or value of the path variable.
  3. To modify the environment variable: type set variable name = variable content for example, to set the java path variable value type set path = C:\jdk\bin or whatever is your address.
  4. To set an empty value: If you want to set a variable as empty, type set variable name = .
  5. To add variables to the variable: Type set variable name =%variable name%; variable content. For example set path =% path%; c:\programfiles\program.exe to add c:\programfiles\program.exe to path. %path% is the environment variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.