How to set up system environment variables in Windows?

Windows uses environment variables to store valuable information about system processes, resource usage, file paths, and more.

Let's learn how to set up system environment variables in Windows.

What are environment variables in Windows?

Environment variables help Windows set up its programming environment and act as information containers for all applications running on the operating system. Imagine an application that wants to know the path to a certain file on your computer. The program can go through the entire system and continue searching until it finds the file. A more innovative way is to call the PATH environment variable containing the paths to all system files.

In addition, system environment variables also affect the installation of the Java Development Kit , Java Runtime Environment, and other required software. As with the examples above, there are many small and large real world use cases for environment variables that generally make Windows a faster version of itself.

Environment Variable Types

Windows creates and uses several environment variables, some of which are user specific and others that are the same for all users on the same computer. We can classify environment variables using user dependency as parameter and call them system environment variables and user environment variables.

Information such as the location of temporary files from an account, the location of your user profile, and so on, is stored in user variables. Windows gives a user account the right to edit user variables, but other user accounts cannot edit them.

In addition, Windows contains system environment variables created by the operating system, programs, drivers, etc. You cannot set system environment variables, but Windows offers the ability to set user environment variable values.

Methods for Setting System Environment Variables in Windows

You can set up system environment variables using various methods in Windows. While the command line methods remain the same for all versions of Windows, the GUI-based methods vary slightly between versions of Windows. Let's take a look at all these methods in detail.

GUI Based Methods

GUI-based methods include using the Start Search menu, the Run window, and the Settings menu to set system environment variables. Let's see how you can access the GUI from different versions of Windows.

Steps for Windows 11

Click on the start menu and search for "environment variables".

The search results will show the option "Edit System Environment Variables". Click on the same and Windows 11 will display a list of all environment variables.

Steps for Windows 10

Go to settings and enter the "About" menu.

Now go to "Advanced system settings". The System Properties dialog box should appear on the screen.

Click the Advanced tab and select Environment Variables. Windows 10 will now display the entire list of user and system variables stored on your computer.

Using the launch window

Press Windows + R to open the Run window.

Now enter the following command:

rundll32.exe sysdm.cpl,EditEnvironmentVariables

All of these methods should open a list of all environment variables, organized into separate sections for user and system environment variables. You can create new user variables, edit existing ones, or delete them using the same dialog box.

Steps to Create a New Environment Variable

Click New in the Environment Variables dialog box.

Now enter the variable name and its value in the appropriate columns and click OK.

Creating the JAVA_HOME environment variable is an important step to install the Java Development Kit. So let's create a JAVA_HOME variable and check for its existence later. Click "Create" and enter "JAVA_HOME" as the variable name. Also, enter the JDK installation path as the variable value for JAVA_HOME.

The JAVA_HOME variable is now visible in the list of all environment variables, and the value of the variable is the path to the JDK. You can verify this by going into the "edit system variable" settings and the JAVA_HOME variable should be present right there.

Steps to Edit Environment Variables

Click on the environment variable you want to change and click Edit.

Now enter a variable name and value, and click OK.

Let's now edit the JAVA_HOME variable we just created and change its value to a different folder. Click on a variable and select the "Edit" option. Now enter a different value for the variable, replacing the previous value, and click OK.

Here you can also check the changed value in the list of environment variables.

The updated variable is present in the list of user variables.

Steps to Remove Environment Variables

Click on the environment variable you want to remove.

Now click "Delete" and click OK.

As an example, let's remove the JAVA_HOME variable we recently tweaked. Select a variable and click "Delete" and "OK" in sequence. The selected variable is removed from the variable list.

The JAVA_HOME variable is removed from the list.

command line method

You can use the command line or Windows PowerShell to set environment variables. Let's first see how to use the command line method.

View environment variables

Open a command prompt in Windows.

Now type "set" and press Enter. You can see the whole list of environment variables without any categorization unlike GUI based method.

Creating new environment variables

Open a command prompt.

Use the following syntax using the setx command and press Enter:

setx [variable_name] “[variable_value]”

[variable_name] is the name of the variable you want to enter.

[variable_value] denotes the value of the newly created variable.

For example, let's create "TEST_VARIABLE" with value "XYZ" and then check its existence using the command line. We use the following command:

setx [TEST_VARIABLE] “[XYZ]”

Congratulations! You have just created a new user variable using the command line. Now let's check its existence. Use the "set" command to see a list of all variables.

The method is Windows PowerShell

PowerShell gives you more flexibility with environment variables and allows you to view, edit, and create them, but they are only valid for a single PowerShell session. The list of variables returns to its original form after the PowerShell session is closed.

Viewing System Variables

Open Windows PowerShell.

Now enter the following command:

Get-ChildItem Env:

Windows PowerShell will display a complete list of environment variables.

Environment variables will not be classified as system and user variables, but you can only use the following commands to view system variables with PowerShell:

[Environment]::GetEnvironmentVariables("Machine")

Otherwise, you can use the following command to view only the user's environment variables:

[Environment]::GetEnvironmentVariables("User")

Creating and editing environment variables

You can edit and create new environment variables using the $env built-in variable. Use the following command to create a new variable using PowerShell:

$env:Variable_name="Variable_value"

Here Variable_name denotes the name of the newly created environment variable and variable_value denotes its value.

Let's create another test variable TEST_VARIABLE as an example and then check for its existence. We use the following command in PowerShell:

$env:TEST_VARIABLE = '[ABC]'

We have also validated the value of the variable for TEST_VARIABLE using the following code:

$env:TEST_VARIABLE

PowerShell shows the output for the TEST_VARIABLE variable as [ABC].

Alternatively, you can also set the value to an existing environment value using the following command:

$env:Variable_name=";Variable_value2"

This will add the newly mentioned value to the original value of the environment variable.

Output 🧑‍💻

Creating and configuring system environment variables is critical to managing programs and using their features. Windows gives you GUI and command line options to do the same. GUI based methods are simple and easy to use. On the other hand, command line methods are faster but more difficult.

Now you can check Tuning MySQL system variables for high performance.

Related posts