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.
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
If you are using Windows PowerShell, list all the environment variables with:
Get-ChildItem Env:
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]%
In Windows PowerShell, use:
echo $Env:[variable_name]
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.
3. Open the Advanced tab and click on the Environment Variables button in the System Properties window.
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.
5. Enter the variable name and value in the New User Variable prompt and click OK.
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"
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
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 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
List all the system environment variables with:
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment"
If you want to list a specific variable, use:
reg query HKEY_CURRENT_USEREnvironment /v [variable_name]
or
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name]
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
or
reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name] /f
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.
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
1C: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 3HKEY_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
1The 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
1ERROR: 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
1SUCCESS: 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
To make the environment variable accessible globally you need to set it in the registry. As you’ve realised by just using:
set NEWVAR=SOMETHING
you are just setting it in the current process space.
According to this page you can use the setx
command:
setx NEWVAR SOMETHING
setx
is built into Windows 7, but for older versions may only be available if you install the Windows Resource Kit
nhinkle
36.9k36 gold badges137 silver badges177 bronze badges
answered Dec 6, 2009 at 21:58
ChrisFChrisF
41.1k17 gold badges97 silver badges152 bronze badges
5
We can also use «setx var variable /M» to set the var to system environment variable level instead of user level.
Note: This command should be run as administrator.
music2myear
39.7k41 gold badges83 silver badges127 bronze badges
answered Aug 26, 2014 at 10:37
Minh ChauMinh Chau
4914 silver badges2 bronze badges
You can use setx env var [/M]
as mentioned above.
If it doesn’t take effect you can use refreshenv
to refresh environment variables. You don’t have to restart your computer, explorer.exe or your command prompt to do that.
Edit: apparantly refreshenv doesn’t come naturally with Windows, so here’s the source: https://pastebin.com/1fJqA0pT
Save as RefreshEnv.cmd and place it in a folder that’s included in your PATH environment variables
answered Nov 13, 2018 at 19:42
DFSFOTDFSFOT
2112 silver badges5 bronze badges
1
System variables can be set through CMD and registry
For ex. reg query «HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment» /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.
answered Mar 9, 2017 at 5:06
2
I want to add that if you are using the /s parameter with setx in order to set environment variables on a remote computer, the «Remote Registry» service needs to be running on the target machine or else you will receive a «ERROR: The specified operation could not be completed.»
(I have asked Microsoft to update their TechNet article on setx to include this information.)
answered May 18, 2017 at 19:35
Tim BailenTim Bailen
911 silver badge3 bronze badges
An example with VBScript (.vbs)
Sub sety(wsh, action, typey, vary, value)
Dim wu
Set wu = wsh.Environment(typey)
wui = wu.Item(vary)
Select Case action
Case "ls"
WScript.Echo wui
Case "del"
On Error Resume Next
wu.remove(vary)
On Error Goto 0
Case "set"
wu.Item(vary) = value
Case "add"
If wui = "" Then
wu.Item(vary) = value
ElseIf InStr(UCase(";" & wui & ";"), UCase(";" & value & ";")) = 0 Then
wu.Item(vary) = value & ";" & wui
End If
Case Else
WScript.Echo "Bad action"
End Select
End Sub
Dim wsh, args
Set wsh = WScript.CreateObject("WScript.Shell")
Set args = WScript.Arguments
Select Case WScript.Arguments.Length
Case 3
value = ""
Case 4
value = args(3)
Case Else
WScript.Echo "Arguments - 0: ls,del,set,add; 1: user,system, 2: variable; 3: value"
value = "```"
End Select
If Not value = "```" Then
' 0: ls,del,set,add; 1: user,system, 2: variable; 3: value
sety wsh, args(0), args(1), UCase(args(2)), value
End If
What is an environment variable in Windows? An environment variable is a dynamic “object” containing an editable value which may be used by one or more software programs in Windows.
In this note i am showing how to set an environment variable in Windows from the command-line prompt (CMD) and from the Windows PowerShell.
In the examples below i will set an environment variable temporary (for the current terminal session only), permanently for the current user and globally for the whole system.
Cool Tip: Add a directory to Windows %PATH%
environment variable! Read More →
Set Environment Variable For The Current Session
Set an environment variable for the current terminal session:
# Windows CMD C:> set VAR_NAME="VALUE" # Windows PowerShell PS C:> $env:VAR_NAME="VALUE"
Print an environment variable to the console:
# Windows CMD C:> echo %VAR_NAME% # Windows PowerShell PS C:> $env:VAR_NAME
Cool Tip: List Windows environment variables and their values! Read More →
Set Environment Variable Permanently
Run as Administrator: The setx
command is only available starting from Windows 7 and requires elevated command prompt. It works both for the Windows command-line prompt (CMD) and the Windows PowerShell.
Permanently set an environment variable for the current user:
C:> setx VAR_NAME "VALUE"
Permanently set global environment variable (for all users):
C:> setx /M VAR_NAME "VALUE"
Info: To see the changes after running setx
– open a new command prompt.
Introduction
In Windows, you sometimes need to modify environment variables.
There are environment variables like %APPDATA%
and %PROGRAMFILES%
which contain useful paths, and others that contain things like
your username (%USERNAME%
). An important one that you may
want to modify is %PATH%
. In this guide we will look at
how to set, check, update, and unset environment variables
using the GUI and the command prompt.
Note that environment variables are case-insentive and that
there are system-wide environment variables
and user-specific environment variables.
Using GUI
You can edit the environment through the Windows settings
You can get to the GUI a few ways:
- Press Windows key and search for «environment» and click
Edit the system environment variables
from the Control Panel. - Press Windows key+R to directly run:
SystemPropertiesAdvanced.exe
Once in the System Properties panel, click the «Environment Variables…» button in the bottom-right.
From the GUI, you can edit the user variables on the top and the system variables on the bottom.
After setting an environment variable using this GUI method,
you will need to restart your applications for it to take effect.
Using command prompt
Use set
and setx
to manipulate environment variables from the command line.
Get help with set /?
and setx /?
. Let’s look at what each one does.
Check an environment variable
Check all environment variables with set
:
set
Check a specific variable like this:
set X
You can print out an environment variable like this:
echo %X%
Set environment variable for a single session
To set an environment variable just for the current command prompt, use set
.
set X=something
Permanently set environment variable
To permanently set an environment variable that will persist across
command prompts and through restart, use setx
. Get help with setx /?
.
setx X 123
By default, this is a user environment variable, not a system one.
To set a system environment variable, add the /M
flag (will require admin prompt).
A common scenario is the need to update your PATH.
In this situation you want to add to the existing variable.
You can do that by referencing the original value like this:
setx PATH "%PATH%;C:optbin"
Unset environment variable
To unset an environment variable, set to to an empty string.
set X=
or
setx X ""
Conclusion
After reading this, you should know how to set environment variables in Windows
permanently or for a single session using the GUI or the command prompt.
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.
- 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.
- Click the Advanced system settings item in the popup window left side, it will popup the System Properties window.
- Click the Environment Variables… button at the bottom-right of the System Properties window, it will popup the Environment Variables window.
- 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.
- 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.
- 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.
- 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.
- 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; ......
- 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;
- 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
- 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.
- We can run
setx
command in both Dos window and Windows PowerShell to set environment variables in the command line. The command format issetx 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.
- 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.
- To see the changes, you need to open a new Dos window or PowerShell window and run the command
echo %HELLO%
orecho %ABC%
.C:Userssong zhao>echo %HELLO% 'haha' C:Userssong zhao>echo %ABC% C:
- 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. - 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.