How to add environment variables windows

This wikiHow teaches how to add new environment variables in Windows 10. Environment variables are useful to specify paths internally in the OS for specific programs. It is basically stored in the form of a name and value pair. OS has a...


Download Article


Download Article

This wikiHow teaches how to add new environment variables in Windows 10. Environment variables are useful to specify paths internally in the OS for specific programs. It is basically stored in the form of a name and value pair. OS has a lot of builtin environment variables like ‘PATH’ where paths to installed Softwares are stored. Stick to this article, to find out how to add environment variables in Windows 10.

Steps

  1. Image titled 1_locate_desktop_screen

    1

    Locate the «This PC» icon on your system. You may find it on your desktop screen. You can go to your desktop by pressing Ctrl+D.

  2. 2

    Right-click on the «This PC» icon. This will show a small popup, with few options.

    Advertisement

  3. Image titled 2_click_properties

    3

    Click on the «Properties» option. You may find this in the last position below the «Rename» option. This will open a new window with all the system details like RAM, Processor, OS, etc.

  4. Image titled 3_click_advanced_settings

    4

    Click or double click on «Advanced System Settings». You may find this option on the left-hand side of the newly opened window (System Details). This option is below the «System Protection» option.

  5. Image titled 4_click_environment_variables.png

    5

    Click on Environment Variables. After clicking, it will show a new window dialog called «Environment Variables».

  6. Image titled 5_decide_variables.png

    6

    Decide which variable to add (User / System). Click the New option in any (User/System) section.

  7. Image titled 6_add_variables.png

    7

    Add the variable name and variable value and click OK. In variable_name, you can give a name to your user variable and specify PATH in the variable_value, finally, click «ok» to save the changes.

  8. Image titled 7_view_variables

    8

    View the environment variables. After saving a new variable with path, you can see the entry of variables.

  9. Image titled 8_add_new_vaiable

    9

    Click OK. After clicking OK, you will have successfully created a new User variable with a PATH attached to it.

  10. Advertisement

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

Thanks for submitting a tip for review!

About This Article

Thanks to all authors for creating a page that has been read 13,993 times.

Is this article up to date?

Introduction

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 variables in Windows

Prerequisites

  • A system running Windows 10
  • User account with admin privileges
  • Access to the Command Prompt or Windows PowerShell

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:

set
List all environment variables using the Command Prompt

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

Get-ChildItem Env:
List all environment variables using Windows PowerShell

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:

echo %[variable_name]%
Checking a specific environment variable using the Command Prompt

In Windows PowerShell, use:

echo $Env:[variable_name]
Checking a specific environment variable using Windows PowerShell

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

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.

Run sysdm.cpl

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

Find the Environment Variables button in the Advanced tab

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.

Click on the New... button to add a variable

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

Enter the new variable name and value

Set Environment Variable in Windows via Command Prompt

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

setx [variable_name] "[variable_value]"

Where:

  • [variable_name]: The name of the environment variable you want to set.
  • [variable_value]: The value you want to assign to the new environment variable.

For instance:

setx Test_variable "Variable value"
Setting a user-specific environment variable via the Command Prompt

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:

setx [variable_name] "[variable_value]" /M
Setting a system environment variable via the Command Prompt

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.

In this 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.

Unset environment variables in Windows via GUI

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:

  • user-specific variables: HKEY_CURRENT_USEREnvironment
  • system-wide variables: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment

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:

reg query HKEY_CURRENT_USEREnvironment
Listing all user-specific environment variables in the registry

List all the system environment variables with:

reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment"
Listing all system environment variables in the registry

If you want to list a specific variable, use:

reg query HKEY_CURRENT_USEREnvironment /v [variable_name]
Listing a specific user environment variable in the registry

or

reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name]
Listing a specific system environment variable in the registry

Where:

  • /v: Declares the intent to list a specific variable.
  • [variable_name]: The name of the environment variable you want to list.

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

reg delete HKEY_CURRENT_USEREnvironment /v [variable_name] /f
Unsetting a user-specific environment variable from the registry

or

reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name] /f
Unsetting a system environment variable from the registry

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:

setx [variable_name] trash

Conclusion

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.

If by «system environment variables» you refer specifically to system-wide environment variables, then other answers have already covered this. However, if you want to edit both system-wide and user-specific environment variables then most (if not all) of these answers are inapplicable in general case.

Going through «System» and then “Advanced system settings” -> “Environment Variables” will only work for accounts from Administrators group, because only such accounts have access to “Advanced system settings”.

If you attempt do that from a regular user account, then trying to access “Advanced system settings” will trigger an UAC prompt asking you for administrator password. If you enter the password, “Advanced system settings” will successfully open, but any user-specific changes you make there will apply to the corresponding administrator’s account (!), not to your original user’s account.

In order to solve this problem (i.e. in order to give regular users the opportunity to edit their own environment variables) Windows provides another way to access the “Environment Variables” dialog.

Open Control Panel. Open User Accounts applet. On the left-hand side of that applet you will see a link that says Change my environment variables. Click that link, and it will take you to the same “Environment Variables” dialog for your user’s environment variables.

enter image description here

If your user has administrator rights, you will be able to edit both sections of that dialog: user-specific environment variables (upper section) and system-wide environment variables (lower section). If you don’t have administrator rights, you will only be able to edit the upper section: your own user-specific environment variables.

This is the proper way to edit environment variables in all post-UAC versions of Windows, not what is suggested in the majority of the answers above.


Unfortunately, Windows 10 November update (version 1511) destroyed this functionality. The Change my environment variables link no longer works. It is there, but it is dead. So for the post-November version of Windows 10 the correct answer is: it is generally impossible to edit user-specific environment variables in version 1511 of Windows 10 from regular user accounts. Microsoft has destroyed Windows 10 with this update and Windows 10 is now unusable. It will remain the case until they fix these ridiculous bugs in 1511 version of the OS.

For the time being one workaround for non-administrative accounts is to, well, add your user account to Administrators group, logout, log back in, edit the variables using «System» -> “Advanced system settings” method, and then take away administrative rights again…

An alternative workaround is to use PowerShell features as described here
https://technet.microsoft.com/en-us/library/ff730964.aspx


Windows 10 Anniversary Update (version 1607) released August 2, 2016 finally fixed this bug.

Learn how to add or edit environment variables in Windows to make your development work easier and shorter.

Normally, in windows an application is not saved into the PATH variable of the system for accessing it easily in the command prompt unless the setup (installer) provides this option.

For example, if you try to use an application that provides a console interface without define it as an environment variable, you’ll need to provide the entire path to the executable :

C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe http://google.com google.pdf

That’s a lot of typing (the entire executable path C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe while in other platforms you can execute wkhtmltopdf http://google.com google.pdf) in the command prompt, especially for something that as developer, you have to run often.

Fortunately, you can customize this behaviour creating environment variables by yourself.

What are environment variables

An Environment variable is an identifier that has a value related to the current environment, like the Operating System or user sessions, it in most of the cases is an absolute path (C:pathtosomething).

After include i.e the EXECUTABLE path in the PATH variable of the system, then you’ll be able to invoke the executable directly in the console without provide the full path but only its name:

wkhtmltopdf http://google.com google.pdf

Better, isn’t ? However, note that is not bad to use the full path, we just create the environment variable to write lessFor example, typing wkhtmltopdf (the .exe can be omitted) in the command line on Windows will show you the utilities available for wkhtmltopdf.

Note: in this article, we are going to add the wkhtmltopdf application as an environment variable in Windows. You only need to change the absolute path with yours in case you’re adding another environment variable.

Before continue

To add an environment variable and type less, you need to provide the path to the folder where the executable is located, never the path with executable.

REM Wrong
C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe
REM Correct
C:/Program Files/wkhtmltopdf/bin

To open the environment variables menu, press the Windows key to open up the Start Menu or Start Screen, then search for «advanced system settings». Alternatively, you can access this menu if you go to the Control Panel, then Security , finally click on System and click on the Advanced system settings hyperlink in the left hand pane.

Environment variables menu

Once the System Properties window opens, click on the “Environment Variables” button.

Environment variables button

In the System Variables box, look for a variable called Path. Select that and click on the Edit button.

Path environment variable

In 7 and 8, the variable value for Path is nothing more than a long string of text with various locations around the system. In order to add an entry to your path in Windows 7 and 8, you have to precede the new folder with a semicolon, so:

;C:/Program Files/wkhtmltopdf/bin

Environment variable windows 7 and 8

Click ok and you’re ready to go.

Windows 10

To open the Environment variables menu, go to Control Panel > System and Security and finally in System. Click on the Advanced System Settings in the left panel.

System advanced properties

Alternatively, you can open directly the environment variables menu by pressing Windows + R (Run app) and then type SystemPropertiesAdvanced.exe :

Windows 10 run

The System Properties dialog will be shown, to continue click on Environment Variables in the Advanced Tab.

Windows 10 environment variables

The Environment variables menu will be shown, select the Path variable in the User variables area and click on Edit.

Edit the path variable and add the new path

Finally click OK in the Edit environment variable menu and you are ready to go,now you’ll be able to write just the name of the executable (without path and extension) anywhere in the command prompt, i.e wkhtmltopdf.

Open a command prompt and try accesing the executable from any path in the system without type the full path:

Environment variable

Note: remember that you need to open a new command prompt to access the variable. If you have an active window of cmd.exe then close it, and open again and the variable will be available.

Updated: 12/05/2021 by

Setting the path and environment variables differs depending on the Windows operating system version on your computer. Select a link below for your version of Windows and follow the steps.

Note

Administrator privileges are required to modify the path and environment variables.

Setting the path and variables in Windows 11

  1. Press the Windows key+X to access the Power User Task Menu.
  2. In the Power User Task Menu, select the System option.
  3. In the System window, scroll to the bottom and click the About option.
  4. In the System > About window, click the Advanced system settings link at the bottom of the Device specifications section.
  5. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab.
  6. In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each directory path is separated with a semicolon, as shown below.
C:Program Files;C:Winnt;C:WinntSystem32

Windows 10 environmental path settings

  1. After creating or modifying the environment variables, restart the computer for those changes to take effect in Windows.

Note

You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New, and enter the variable name and value.

Tip

To view and set the path through the Windows command line, use the path command.

Setting the path and variables in Windows 10

  1. Press the Windows key+X to access the Power User Task Menu.
  2. In the Power User Task Menu, select the System option.
  3. In the About window, click the Advanced system settings link under Related settings on the far-right side.
  4. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab.
  5. In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each directory path is separated with a semicolon, as shown below.
C:Program Files;C:Winnt;C:WinntSystem32

Windows 10 environmental path settings

  1. After creating or modifying the environment variables, restart the computer for those changes to take effect in Windows.

Note

You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New, and enter the variable name and value.

Tip

To view and set the path through the Windows command line, use the path command.

Setting the path and variables in Windows 8

  1. Press the Windows key+X to access the Power User Task Menu.
  2. In the Power User Task Menu, select the System option.
  3. Click the Advanced System Settings link in the left column.
  4. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab.
  5. In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
C:Program Files;C:Winnt;C:WinntSystem32

Windows environmental path settings

  1. After creating or modifying the environment variables, restart the computer for those changes to take effect in Windows.

Note

You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New, and enter the variable name and value.

Tip

To view and set the path through the Windows command line, use the path command.

Setting the path and variables in Windows Vista and Windows 7

  1. On the desktop, right-click the Computer icon and select Properties. If you don’t have a Computer icon on your desktop, click Start, right-click the Computer option in the Start menu, and select Properties.
  2. Click the Advanced System Settings link in the left column.
  3. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab.
  4. In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
C:Program Files;C:Winnt;C:WinntSystem32

Windows environmental path settings

  1. After creating or modifying the environment variables, restart the computer for those changes to take effect in Windows.

Note

You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New, and enter the variable name and value.

Tip

To view and set the path through the Windows command line, use the path command.

Setting the path and variables in Windows 2000 and Windows XP

The path is now managed by Windows 2000 and Windows XP and not the autoexec.bat or autoexec.nt files, as was done with earlier versions of Windows. To change the system environment variables, follow the steps below.

  1. From the desktop, right-click My Computer and click Properties. If you don’t have a My Computer icon on your desktop, click Start, right-click the My Computer option in the Start menu, and select Properties.
  2. In the System Properties window, click the Advanced tab.
  3. In the Advanced section, click the Environment Variables button.
  4. In the Environment Variables window (as shown below), highlight the Path variable in the System Variable section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
C:Program Files;C:Winnt;C:WinntSystem32

Windows environmental path settings

  1. After creating or modifying the environment variables, restart the computer for those changes to take effect in Windows.

Note

You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New, and enter the variable name and value.

Tip

To view and set the path through the Windows command line, use the path command.

What is the default Windows %PATH%?

The path is based on programs installed on the computer, so there is no «default path.» However, the Windows minimum path is often the path below.

%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem

Note

Keep in mind that as you install programs, the path is updated with the paths for the newly installed programs. So, if you have erased your path after installing other programs, those programs may be affected.

Setting path in the MS-DOS and Windows command line

To view and set the path in MS-DOS and the Windows command line, use the path command.

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

Command Prompt — C:>

Output

1
2
3
4
5
6
ALLUSERSPROFILE=C:ProgramData
APPDATA=C:UsersuserAppDataRoaming
.
.
.
windir=C:Windows

Print a particular environment variable:

Command Prompt — C:>

Output

Windows PowerShell

List all environment variables

Windows PowerShell — PS C:>

Output

1
2
3
4
5
6
7
8
Name                           Value
----                           -----
ALLUSERSPROFILE                C:ProgramData
APPDATA                        C:UsersuserAppDataRoaming
.
.
.
windir                         C:Windows

Print a particular environment variable:

Windows PowerShell — PS C:>

Output

Set Environment Variables

To set persistent environment variables at the command line, we will use setx.exe. It became part of Windows as of Vista/Windows Server 2008. Prior to that, it was part of the Windows Resource Kit. If you need the Windows Resource Kit, see Resources at the bottom of the page.

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

User Variables

Command Prompt — C:>

1
setx EC2_CERT "%USERPROFILE%awscert.pem"

Open a new command prompt.

Command Prompt — C:>

Output

1
C:Usersuserawscert.pem

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.

Command Prompt — C:>

1
setx EC2_HOME "%APPDATA%awsec2-api-tools" /M

Warning This method is recommended for experienced users only.

The location of the user variables in the registry is: HKEY_CURRENT_USEREnvironment. The location of the system variables in the registry is: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment.

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.

One way is to get this message issued is to open the environment variables in the GUI, like we do in HowTo: Set an Environment Variable in Windows — GUI; we do not need to change anything, just open the Environment Variables window where we can see the environment variables, then hit OK.

Another way to get the message issued is to use setx, this allows everything to be done on the command line, however requires setting at least one environment variable with setx.

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.

First we’ll show the user variables:

Command Prompt — C:>

1
reg query HKEY_CURRENT_USEREnvironment

Output

1
2
3
HKEY_CURRENT_USEREnvironment
    TEMP    REG_EXPAND_SZ    %USERPROFILE%AppDataLocalTemp
    TMP    REG_EXPAND_SZ    %USERPROFILE%AppDataLocalTemp

We can show a specific environment variable by adding /v then the name, in this case we’ll do TEMP:

Command Prompt — C:>

1
reg query HKEY_CURRENT_USEREnvironment /v TEMP

Output

1
2
HKEY_CURRENT_USEREnvironment
    TEMP    REG_EXPAND_SZ    %USERPROFILE%AppDataLocalTemp

Now we’ll list the system environment variables:

Command Prompt — C:>

1
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment"

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment
    ComSpec    REG_EXPAND_SZ    %SystemRoot%system32cmd.exe
    FP_NO_HOST_CHECK    REG_SZ    NO
    NUMBER_OF_PROCESSORS    REG_SZ    8
    OS    REG_SZ    Windows_NT
    Path    REG_EXPAND_SZ    C:ProgramDataOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;%SYSTEMROOT%System32WindowsPowerShellv1.0
    PATHEXT    REG_SZ    .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    PROCESSOR_ARCHITECTURE    REG_SZ    AMD64
    PROCESSOR_IDENTIFIER    REG_SZ    Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
    PROCESSOR_LEVEL    REG_SZ    6
    PROCESSOR_REVISION    REG_SZ    3c03
    PSModulePath    REG_EXPAND_SZ    %SystemRoot%system32WindowsPowerShellv1.0Modules;C:Program FilesIntel
    TEMP    REG_EXPAND_SZ    %SystemRoot%TEMP
    TMP    REG_EXPAND_SZ    %SystemRoot%TEMP
    USERNAME    REG_SZ    SYSTEM
    windir    REG_EXPAND_SZ    %SystemRoot%

And same as with the user variables we can query a specific variable.

Command Prompt — C:>

1
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v PATH

Output

1
2
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment
    PATH    REG_EXPAND_SZ    C:ProgramDataOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;%SYSTEMROOT%System32WindowsPowerShellv1.0

Unsetting a Variable

When setting environment variables on the command line, setx should be used because then the environment variables will be propagated appropriately. However one notable thing setx doesn’t do is unset environment variables. The reg tool can take care of that, however another setx command should be run afterwards to propagate the environment variables.

The layout for deleting a user variable is: reg delete HKEY_CURRENT_USEREnvironment /v variable_name /f. If /f had been left off, we would have been prompted: Delete the registry value EXAMPLE (Yes/No)?. For this example we’ll delete the user variable USER_EXAMPLE:

Command Prompt — C:>

1
reg delete HKEY_CURRENT_USEREnvironment /v USER_EXAMPLE /f

Output

1
The operation completed successfully.

Deleting a system variable requires administrator privileges. See HowTo: Open an Administrator Command Prompt in Windows to see how to do this.

The layout for deleting a system variable is: reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v variable_name /f. For this example we’ll delete the system variable SYSTEM_EXAMPLE:

Command Prompt — C:>

1
reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v SYSTEM_EXAMPLE /f

If this was run as a normal user you’ll get:

Output

1
ERROR: Access is denied.

But run in an administrator shell will give us:

Output

1
The operation completed successfully.

Finally we’ll have to run a setx command to propagate the environment variables. If there were other variables to set, we could just do that now. However if we were just interested in unsetting variables, we will need to have one variable left behind. In this case we’ll set a user variable named throwaway with a value of trash

Command Prompt — C:>

Output

1
SUCCESS: Specified value was saved.

Resources

  • Windows XP Service Pack 2 Support Tools
  • Windows Server 2003 Resource Kit Tools
  • Reg — Edit Registry | Windows CMD | SS64.com
  • Reg — Microsoft TechNet
  • Registry Value Types (Windows) — Microsoft Windows Dev Center
  • How to propagate environment variables to the system — Microsoft Support
  • WM_SETTINGCHANGE message (Windows) — Microsoft Windows Dev Center
  • Environment Variables (Windows) — Microsoft Windows Dev Center

Windows Server 2003 Resource Kit Tools will also work with Windows XP and Windows XP SP1; use Windows XP Service Pack 2 Support Tools with Windows XP SP2. Neither download is supported on 64-bit version.

Parts in this series

  • HowTo: Set an Environment Variable in Windows

  • HowTo: Set an Environment Variable in Windows — GUI

  • HowTo: Set an Environment Variable in Windows — Command Line and Registry

Windows environment variables are very important, it plays an important role in Windows OS. This article will tell you how to set windows environment variables both on GUI and command line.

1. Set Windows Environment Variables On GUI.

  1. Open a Windows Explorer, right-click This-PC on the left side, then click the Properties menu item in the popup menu list, then it will popup the System window.
    windows-10-right-click-this-pc-properties-menu
  2. Click the Advanced system settings item in the popup window left side, it will popup the System Properties window.
  3. Click the Environment Variables… button at the bottom-right of the System Properties window, it will popup the Environment Variables window.
    windows-10-advanced-system-settings-environment-variables-edit-window
  4. There are two parts in the Environment Variables dialog. The upper part area is the User variables ( only available to this user ) list, the lower part area is the System variables ( available to all users that use this Windows OS ) list.
  5. Click the New… button to add a new environment variable, input variable name ( such as CLASSPATH), and variable value ( such as C: ) in the popup dialog, click the OK button to save it.
  6. To edit a windows environment variable, just select it and click the Edit… button. For example, we select the system variable Path and click the Edit… button, then you can add a new path value or edit the existing path value in the popup dialog.
  7. The variable name is just a string, and if one environment variable has multiple values, you should use ; to separate them.

2. Set Windows Environment Variables In Command-Line.

If you want to get and set windows environment variables in a shell script, you had better use the command line to process it.

2.1 Print Windows Environment Variables In Command-Line.

  1. Run the command set in the Dos window to show current environment variables.
    C:WorkSpace>set
    ALLUSERSPROFILE=C:ProgramData
    ......
    OS=Windows_NT
    Path=C:Program Files (x86)Common FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;
    ......
  2. Run the command  echo %variable-name% in the Dos window to print the special environment variable value.
    C:WorkSpace>echo %PATH%
    C:Program Files (x86)Common FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;
  3. Run command Get-ChildItem Env: in the Windows PowerShell window to print all environment variables.
    PS C:Userssong zhao> Get-ChildItem Env:
    
    Name                           Value
    ----                           -----
    ALLUSERSPROFILE                C:ProgramData
    APPDATA                        C:Userssong zhaoAppDataRoaming
    CommonProgramFiles             C:Program FilesCommon Files
  4. Run command echo $Env:variable-name in Windows PowerShell window to print special environment variable value.
    PS C:Userssong zhao> echo $Env:PATH
    C:Program Files (x86)Common FilesOracleJavajavapath;

2.2 Set Windows Environment Variables In Command-Line.

  1. We can run setx command in both Dos window and Windows PowerShell to set environment variables in the command line. The command format is setx variable-name variable-value.
    C:WorkSpace>setx HELLO 'haha'
    
    SUCCESS: Specified value was saved.
    -------------------------------------------
    PS C:Userssong zhao> setx ABC "C:"
    
    SUCCESS: Specified value was saved.
  2. If the user environment variable does not exist, then it will add a new one. If the user environment variable name already exists, then it will overwrite it.
  3. To see the changes, you need to open a new Dos window or PowerShell window and run the command echo %HELLO% or echo %ABC% .
    C:Userssong zhao>echo %HELLO%
    'haha'
    
    C:Userssong zhao>echo %ABC%
    C:
  4. If you want to set a system environment variable, you should first run Dos window or PowerShell window as administrator and then run the command setx variable-name variable-value /M, otherwise, you will encounter the error message ERROR: Access to the registry path is denied.
  5. In the below example, we use windows GUI to add a system environment variable TEST_VAR first, then we want to overwrite it in the command line. Because the Dos window is not opened as administrator, then an error message is thrown.
    PS C:Userssong zhao> setx TEST_VAR "C:" /M
    ERROR: Access to the registry path is denied.

We can also set environment variables in Windows registry, we will introduce it later in this article.

Понравилась статья? Поделить с друзьями:
  • How to add chromedriver to path windows
  • How to add app to startup windows 10
  • How to add an exclusion to windows defender
  • How to add a font to windows
  • How to activate windows with kms