The Computer Oracle

Setting and getting Windows environment variables from the command prompt?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Realization

--

Chapters
00:00 Question
00:45 Accepted answer (Score 343)
01:25 Answer 2 (Score 47)
01:44 Answer 3 (Score 11)
02:23 Answer 4 (Score 6)
03:06 Thank you

--

Full question
https://superuser.com/questions/79612/se...

Question links:
[Is there any command line tool that can be used to edit environment variables in Windows?]: https://superuser.com/questions/13783/
[Setting multiple environment variables in a shell spawned via windows batch script]: https://superuser.com/questions/23048/

Accepted answer links:
[this page]: http://ss64.com/nt
[setx]: http://ss64.com/nt/setx.html
[Windows Resource Kit]: http://ss64.com/links/windows.html#kits

Answer 3 links:
https://pastebin.com/1fJqA0pT

Answer 4 links:
[Set Windows system environment variables using CMD]: http://www.mindxmaster.com/2017/03/modif...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #commandline #environmentvariables

#avk47



ACCEPTED ANSWER

Score 359


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




ANSWER 2

Score 50


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.




ANSWER 3

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




ANSWER 4

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