Environment Variables On Windows 10 - Complete Guide
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.
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.
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.
We'll get to an example shortly.
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.
This is only for the sake of this example. DO NOT try this with the path variable, or you can mess up your computer.
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.
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).
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: