How to set environment variable in windows

How to set environment variable in windows

Environment variables are not often seen directly when using Windows. However there are cases, especially when using the command line, that setting and updating environment variables is a necessity. In this series we talk about the various approaches we can take to set them. In this article we look at how to interface with environment variables using the Command Prompt and Windows PowerShell. We also note where in the registry the environment variables are set, if you needed to access them in such a fashion.

Print environment variables

You can use environment variables in the values of other environment variables. It is then helpful to be able to see what environment variables are set already. This is how you do it:

Command Prompt

List all environment variables

Output

Print a particular environment variable:

Output

Windows PowerShell

List all environment variables

Output

Print a particular environment variable:

Output

Set Environment Variables

setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts.

User Variables

Open a new command prompt.

Output

System Variables

To edit the system variables, you’ll need an administrative command prompt. See HowTo: Open an Administrator Command Prompt in Windows to see how.

Registry

When setting environment variables through the registry, they will not recognized immediately. One option is to log out and back in again. However, we can avoid logging out if we send a WM_SETTINGCHANGE message, which is just another line when doing this programatically, however if doing this on the command line it is not as straightforward.

Printing Environment Variables

With Windows XP, the reg tool allows for accessing the registry from the command line. We can use this to look at the environment variables. This will work the same way in the command prompt or in powershell. This technique will also show the unexpanded environment variables, unlike the approaches shown for the command prompt and for powershell.

How to Setup System Environment Variables in Windows?

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Windows uses environment variables to store valuable information about system processes, resource usage, file path, 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 a software 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 specific file on your computer. The program can either go through the entire system and keep searching until it finds the file. A more innovative way is to call the PATH environment variable that contains the paths to all the system files.

Moreover, system environment variables also affect the installation of Java Development Kit, Java Runtime Environment, and other essential software. Just like the above examples, there are a plethora of small and extensive real-world use cases of environment variables that overall make Windows a snappier version of itself.

Types of Environment Variables

Windows creates and uses several environment variables, some of which are user-dependent and others remain the same for all users on a single machine. We can categorize environment variables using user dependence as a parameter and term them as system environment variables and user environment variables.

Information like the location of temporary files from an account, location of your user profile, etc., are stored under user variables. Windows gives the user account the privilege to edit the user variables, but other user accounts cannot edit them.

Other than this, Windows contains system environment variables created by the operating system, programs, drivers, and more. You cannot tweak the system environment variables, but Windows offers the option to tweak user environment variables’ values.

Methods to Setup System Environment Variables in Windows

You can set up system environment variables using different methods in Windows. Although the command line methods remain unchanged for all Windows versions, the GUI-based methods differ slightly for different Windows versions. Let’s look at all these methods in-depth.

GUI Based Methods

The GUI-based methods include using the Start search menu, the Run Window, and the Settings menu to tweak system environment variables. Let’s look at how you can access the GUI from various Windows versions.

Steps for Windows 11

Click on the Start Menu and search for “environment variables.”

The “Edit the system environment variables” option will appear in the search results. Click on the same, and Windows 11 will display the list of all environment variables.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Steps for Windows 10

Go to settings and enter the “About” menu.

Now go to “Advanced system settings.” The System Properties dialogue box should appear on your screen.

Click on the “Advanced” tab and select “Environment Variables.” Windows 10 will now display the entire list of user and system variables stored on your computer.
How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Using Run Window

Press Windows +R to explore the Run Window.

Now enter the following command:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

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

Steps to create a new environment variable

Click on the “New” option using the Environment Variables dialogue box.

Now enter the Variable Name and its Value under the respective columns and press OK.

Creating the JAVA_HOME environment variable is a crucial step for installing Java Development Kit. So, let’s create the JAVA_HOME variable and later verify its existence. Click on the New option and enter “JAVA_HOME” as the variable name. Also, enter the installation path for the JDK as the variable value for JAVA_HOME.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

The JAVA_HOME variable is now visible on the list of all environment variables, with the variable value as the path to the JDK. You can verify it by going to the “edit system variable” settings, and the JAVA_HOME variable should be present right there.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Steps to Edit environment variables

Click on the environment variable you want to edit and press the “Edit” option.

Now enter the Variable Name and its Value and press the OK button.

Let’s now edit the JAVA_HOME variable that we just created and change its value to another folder. Click on the variable and select the “Edit” option. Now enter a different variable value replacing the previous value and click OK.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Here also, you can verify the changed value on the environment variable list.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

The updated variable is present on the user variable list.

Steps to Delete environment variables

Click on the environment variable you want to Delete.

Now Press the “Delete” option and press OK.

As an example, let’s delete the JAVA_HOME variable that we recently tweaked. Select the variable and press “Delete” and “OK” subsequently. The selected variable gets deleted from the list of variables.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

The JAVA_HOME variable gets deleted from the list.

Command Prompt Method

You can use the Command Prompt or Windows PowerShell to set up environment variables. Let’s first look at how to use the command prompt method.

Viewing the environment variables

Open the command prompt in Windows.

Now enter “set” and press Enter. You can see the entire list of environment variables without any categorization, unlike the GUI-based method.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Creating new environment variables

Open the command prompt.

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

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

[variable_value] stands for the value for the newly created variable.

For example, let’s create a “TEST_VARIABLE” with a value “XYZ” and then verify its existence using Command Prompt. We use the following command:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Congratulations! You just created a new user variable using Command Prompt. Now, let’s verify its existence. Use the “set” command to see the list of all the variables.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Windows PowerShell Method

PowerShell gives you more flexibility with the environment variables and lets you view, edit, and create them, but these are only valid for a single PowerShell session. The variable list returns to its original form once you close a PowerShell session.

Viewing system variables

Open the Windows PowerShell.

Now enter the following command:

Windows PowerShell will display the complete list of environment variables.
How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

The environment variables won’t get categorized under system and user variables, but you can use the following commands to only view system variables using PowerShell:

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

Creating and Editing environment variables

Here Variable_name stands for the name of the newly created environment variable, and the variable_value stands for its value.

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

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

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

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

Moreover, you can also tweak the value for an existing environment value using the following command:

This would append the newly mentioned value to the original value for the environment variable.

Conclusion 🧑‍💻

Creating and tweaking system environment variables is crucial to direct programs and utilize their functionality. Windows gives you GUI-based and Command Line options to do the same. The GUI-based methods are simple and easy to follow. On the other hand, the Command-Line methods are swifter but more complicated.

set (environment variable)

Displays, sets, or removes cmd.exe environment variables. If used without parameters, set displays the current environment variable settings.

This command requires command extensions, which are enabled by default.

The set command can also run from the Windows Recovery Console, using different parameters. For more information, see Windows Recovery Environment (WinRE).

Syntax

Parameters

ParameterDescription
Specifies the environment variable to set or modify.
Specifies the string to associate with the specified environment variable.
/pSets the value of to a line of input entered by the user.
Specifies a message to prompt the user for input. This parameter must be used with the /p parameter.
/aSets to a numerical expression that is evaluated.
Specifies a numerical expression.
/?Displays help at the command prompt.

Remarks

If command extensions are enabled (the default) and you run set with a value, it displays all of the variables that begin with that value.

Use environment variables to control the behavior of some batch files and programs and to control the way Windows and the MS-DOS subsystem appears and works. The set command is often used in the Autoexec.nt file to set environment variables.

If you use the set command without any parameters, the current environment settings are displayed. These settings usually include the COMSPEC and PATH environment variables, which are used to help find programs on disk. Two other environment variables used by Windows are PROMPT and DIRCMD.

If you specify only a variable and an equal sign (without ) for the set command, the value associated with the variable is cleared (as if the variable is not there).

If you use the /a parameter, the following operators are supported, in descending order of precedence:

OperatorOperation performed
( )Grouping
!

If you use logical ( && or || ) or modulus (%) operators, enclose the expression string in quotation marks. Any non-numeric strings in the expression are considered environment variable names, and their values are converted to numbers before they are processed. If you specify an environment variable name that is not defined in the current environment, a value of zero is allotted, which allows you to perform arithmetic with environment variable values without using the % to retrieve a value.

If you run set /a from the command line outside of a command script, it displays the final value of the expression.

Numeric values are decimal numbers unless prefixed by 0Г— for hexadecimal numbers or 0 for octal numbers. Therefore, 0Г—12 is the same as 18, which is the same as 022.

Delayed environment variable expansion support is disabled by default, but you can enable or disable it by using cmd /v.

When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set.

If you call a variable value from a batch file, enclose the value with percent signs (%). For example, if your batch program creates an environment variable named BAUD, you can use the string associated with BAUD as a replaceable parameter by typing %baud% at the command prompt.

Examples

To set an environment variable named INCLUDE so the string c:\directory is associated with it, type:

You can then use the string c:\directory in batch files by enclosing the name INCLUDE with percent signs (%). For example, you can use dir %include% in a batch file to display the contents of the directory associated with the INCLUDE environment variable. After this command is processed, the string c:\directory replaces %include%.

To use the set command in a batch program to add a new directory to the PATH environment variable, type:

To display a list of all of the environment variables that begin with the letter P, type:

Setting a system environment variable from a Windows batch file?

Is it possible to set a environment variable at the system level from a command prompt in Windows 7 (or even XP for that matter). I am running from an elevated command prompt.

When I use the set command ( set name=value ), the environment variable seems to be only valid for the session of the command prompt.

7 Answers 7

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

The XP Support Tools (which can be installed from your XP CD) come with a program called setx.exe :

I think Windows 7 actually comes with setx as part of a standard install.

Simple example for how to set JAVA_HOME with setx.exe in command line:

This will set environment variable «JAVA_HOME» for current user. If you want to set a variable for all users, you have to use option «-m». Here is an example:

Note: you have to execute this command as Administrator.

Note: Make sure to run the command setx from an command-line Admin window

1 like so: setx /M JAVA_HOME «C:\Progra

If you set a variable via SETX, you cannot use this variable or its changes immediately. You have to restart the processes that want to use it.

Use the following sequence to directly set it in the setting process too (works for me perfectly in scripts that do some init stuff after setting global variables):

For XP, I used a (free/donateware) tool called «RAPIDEE» (Rapid Environment Editor), but SETX is definitely sufficient for Win 7 (I did not know about this before).

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

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.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Just in case you would need to delete a variable, you could use SETENV from Vincent Fatica available at http://barnyard.syr.edu/

vefatica. Not exactly recent (’98) but still working on Windows 7 x64.

SetX is the command that you’ll need in most of the cases.Though its possible to use REG or REGEDIT

How to Set Environment Variable in Windows

Home » SysAdmin » How to Set Environment Variable in Windows

Environment variables are key-value pairs a system uses to set up a software environment. The environment variables also play a crucial role in certain installations, such as installing Java on your PC or Raspberry Pi.

In this tutorial, we will cover different ways you can set, list, and unset environment variables in Windows 10.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Check Current Environment Variables

The method for checking current environment variables depends on whether you are using the Command Prompt or Windows PowerShell:

List All Environment Variables

In the Command Prompt, use the following command to list all environment variables:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

If you are using Windows PowerShell, list all the environment variables with:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Check A Specific Environment Variable

Both the Command Prompt and PowerShell use the echo command to list specific environment variables.

The Command prompt uses the following syntax:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

In Windows PowerShell, use:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Here, [variable_name] is the name of the environment variable you want to check.

Set Environment Variable in Windows via GUI

Follow the steps to set environment variables using the Windows GUI:

1. Press Windows + R to open the Windows Run prompt.

2. Type in sysdm.cpl and click OK.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

3. Open the Advanced tab and click on the Environment Variables button in the System Properties window.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

4. The Environment Variables window is divided into two sections. The sections display user-specific and system-wide environment variables. To add a variable, click the New… button under the appropriate section.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

5. Enter the variable name and value in the New User Variable prompt and click OK.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Set Environment Variable in Windows via Command Prompt

Use the setx command to set a new user-specific environment variable via the Command Prompt:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Note: You need to restart the Command Prompt for the changes to take effect.

To add a system-wide environment variable, open the Command Prompt as administrator and use:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Unset Environment Variables

There are two ways to unset environment variables in Windows:

Unset Environment Variables in Windows via GUI

To unset an environment variable using the GUI, follow the steps in the section on setting environment variables via GUI to reach the Environment Variables window.

1. Locate the variable you want to unset in the appropriate section.

2. Click the variable to highlight it.

3. Click the Delete button to unset it.

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Unset Environment Variables in Windows via Registry

When you add an environment variable in Windows, the key-value pair is saved in the registry. The default registry folders for environment variables are:

Using the reg command allows you to review and unset environment variables directly in the registry.

Note: The reg command works the same in the Command Prompt and Windows PowerShell.

Use the following command to list all user-specific environment variables:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

List all the system environment variables with:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

If you want to list a specific variable, use:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Use the following command to unset an environment variable in the registry:

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

How to set environment variable in windows. Смотреть фото How to set environment variable in windows. Смотреть картинку How to set environment variable in windows. Картинка про How to set environment variable in windows. Фото How to set environment variable in windows

Note: The /f parameter is used to confirm the reg delete command. Without it, entering the command triggers the Delete the registry value EXAMPLE (Yes/No)? prompt.

Run the setx command again to propagate the environment variables and confirm the changes to the registry.

Note: If you don’t have any other variables to add with the setx command, set a throwaway variable. For example:

After following this guide, you should know how to set user-specific and system-wide environment variables in Windows 10.

Looking for this tutorial for a different OS? Check out our guides on How to Set Environment Variables in Linux and How to Set Environment Variables in MacOS.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *