TikTekTok Logo

Environment Variables On Windows 10 - Complete Guide






Luis Silva


(Jan. 2020)



 

Gone are the days when we had to know how to set up a variety of advanced parameters on the OS for a computer to work correctly. Nowadays, modern OSes usually take care of that, but on some rare occasions, we do need to get our hands dirty and do it ourselves.


Shortcuts:

What are EVs

How to set them on Windows

How to set them from command line

How to set them from Powershell


What are Environment Variables and when do you need to set them




An environment variable is a dynamic-named value that can affect the way running apps behave on a computer.

An app may need to know the location of a package (or other data) of another app to run accurately.

Therefore they use a predefined environment variable to share this location.

We'll get into some examples right away.


How to set an environment variable on windows.




The quickest way to get there is to press WIN+PAUSE or WIN+BREAK and then on Advanced system settings, click on Environment Variables.


You can also press the WIN key, type view ad, and then under the Best match heading, click on View advanced system settings.


Or you can press WIN+R and type systempropertiesadvanced.


You'll have two types of Environment Variables (EVs), user variables and system variables. The former is a list of values (usually folder names) assigned to your data, while the latter is related to your machine. You then click on New and add a variable name and a Variable value.


Let's try now with a typical example.



How to set Java environment variables on Windows 10 (Java classpath)




Java is a multi-platform programming language and runtime that needs to be available to Java-based apps. With the help of an EV, they know precisely where the runtime executable is.

That's something I still have to do now and then on some machines.

  • Just like we've seen before, press WIN+PAUSE or WIN+BREAK and then on Advanced system settings, click on Environment Variables.

  • Then, in the System variables section, click on the new... button and enter CLASSPATH as the Variable Name.


  • Next, in the Variable Value, enter the path where you've installed the runtime (eg.: c:\javalib;).

  • Click OK. Close all remaining windows by clicking OK, and you're done.



How to set an environment variable from the command line




If you prefer a "copy and paste" solution, there's also an easy way using the Windows command line. Just follow the next set of instructions carefully.

  • Press WIN+R and type cmd to open a command line.


  • Type setx + space + variable name + space + variable value. This adds it to the user environment.

  • If you want to add it to the system environment instead, add /M to the setx command line. eg.:setx /M + space + variable name + space + variable value. You may need to run CMD with administrator privileges.

  • Exit the command line and then open it again.

  • To confirm, type set and search for the new variable name. You can also type echo %variablename%.

We'll get to an example shortly.

  • To remove the variable from the current environment, type set variablename=.

  • To remove it permanently from the user environment, type REG delete HKCU\Environment /F /V variablename.

  • To remove it permanently from the system environment, type REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V variablename.

For this process to take effect, you have to logout and log back in again.



How to add Java to the path variable on Windows 10 (Java classpath)




Lets set an EV from the command line again, with a Java example.

path is one of the most popular variable names on Windows. It defines the location of every app's executable. Whenever the user (or another app) tries to run that app, it can do so, even without knowing where it is located on this particular machine.

You can use the command line to set its value.

Warning!

This is only for the sake of this example. DO NOT try this with the path variable, or you can mess up your computer.

  • Press WIN+R and type cmd and press Ctrl+Shift+Enter (instead of just Enter) to open a command line with administrative privileges.

  • Type setx path "c:\". This replaces every folder in that EV with the PC root folder for the user environment space.

  • Type setx /M path "c:\" to set it for the system environment instead.

  • Exit the command line and then open it again.

  • To confirm, type set and search for the path variable name. You can also type echo %path%.

  • If you want to add a new folder to the path variable, for example, the JAVA_HOME variable, type setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0" (this will create a new EV called JAVA_HOME) and then add it by typing setx path "%path%;%JAVA_HOME%\bin".


Creating a new EV makes it easier to manage the value of the path EV in the future. Whenever you need to upgrade (change) the JDK version, all you need to do is set that new EV value again.

Adding a new folder to path is safer because it doesn't set a new value. Instead, it adds a new variable value. The % enclosure represents the variable value instead of the variable name. The ; is a folder delimiter.

Be aware that C:\Program Files\Java\jdk1.8.0 may not be the Java installation folder on your machine.

To confirm that everything is in order, exit the command line, open it again, and then type echo %path%.

If you found a mistake, remember that REG delete HKCU\Environment /F /V path, or REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V path removes the variable name from your PC. Logout, log back in and start this process all over again. Be aware that this removes the path's old value, so you must add it again as soon as you remove it.



How to set an environment variable on Powershell




If Powershell is your thing, then you can also easily set an EV (or add a new variable value) on it.

  • Start Powershell by pressing WIN+R, then enter powershell, and then press Ctrl+Shift+Enter.


  • Type $env:" + variable name + "=" + variable value. This will create a new EV in the user environment. If you want to check it, just type $env: + variable name and Powershell will return its actual value.

  • Note that the variable value must be inside quotation marks.

  • If you want to add an EV value instead of replacing the old one, then type $env: + variable name + "+=" + variable value.

This isn't permanent. As soon as you close Powershell, the EV will default back to what it was before. If you need a permanent solution, check out the following example.



How to add Python to the path variable on Windows 10 using Powershell




Let's use this popular programming language as an example by adding its executable to the path EV.

After opening Powershell, with administrative privileges, you can type $env:Path += ";C:\Python32" (for version 3.2 of this program), but we already saw that this isn't permanent. So, if you want to write any value to the Windows Registry, try this instead:

[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Python33", "Machine").


This will add a new value to the path EV in the machine (system) environment.

You'll have to restart Powershell for changes to take effect.

In the end, don't forget to do one final check if your EVs have the value that you expected. You can type $env:path to verify. Also, when dealing with the path variable, make sure you are adding (and not replacing) a value. Otherwise, you could be damaging your OS.

I'm still going to give you an example of how to replace a value instead of adding it: $env:path="c:\" (just change path to something else or c:\ to the appropriate value).



Conclusion




I hope I've covered every possible way of changing, adding, and checking EV values. If, for any reason, something did not work for you or you still have any doubts, leave a comment below or contact me and I'll try to help as much as I can.



For more information about EVs, check this Wikipedia link:







Related articles:


7-Zip vs WinZip vs WinRAR vs Windows Comparison


How To Keep Your PC From Slowing Down


How To Test Your USB Flash Drive Speed