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, How to Set Environment Variables in ZSH, and How to Set Environment Variables in MacOS.
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
37.2k36 gold badges140 silver badges177 bronze badges
answered Dec 6, 2009 at 21:58
ChrisFChrisF
41.3k17 gold badges101 silver badges154 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
40.7k44 gold badges86 silver badges127 bronze badges
answered Aug 26, 2014 at 10:37
Minh ChauMinh Chau
5014 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_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.
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
You must log in to answer this question.
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.
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:\Users\user\AppData\Roaming
.
.
.
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:\Users\user\AppData\Roaming
.
.
.
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:\>
1setx EC2_CERT "%USERPROFILE%\aws\cert.pem"
Open a new command prompt.
Command Prompt — C:\>
Output
1
C:\Users\user\aws\cert.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%\aws\ec2-api-tools" /M
Warning This method is recommended for experienced users only.
The location of the user variables in the registry is: HKEY_CURRENT_USER\
. The location of the system variables in the registry is: HKEY_LOCAL_MACHINE\
.
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_USER\Environment
Output
1
2
3
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
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_USER\Environment /v TEMP
Output
1
2
HKEY_CURRENT_USER\Environment
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
Now we’ll list the system environment variables:
Command Prompt — C:\>
1
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
ComSpec REG_EXPAND_SZ %SystemRoot%\system32\cmd.exe
FP_NO_HOST_CHECK REG_SZ NO
NUMBER_OF_PROCESSORS REG_SZ 8
OS REG_SZ Windows_NT
Path REG_EXPAND_SZ C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.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%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Intel\
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_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
Output
1
2
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
PATH REG_EXPAND_SZ C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.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_USER\
. 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_USER\Environment /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_MACHINE\
. For this example we’ll delete the system variable SYSTEM_EXAMPLE
:
Command Prompt — C:\>
1
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /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
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.
Was it useful? Share this post with the world!
Windows Command Line – Set Environment Variable
The windows command line or text interface is a kind of user interface that can be controlled by passing commands in the form of prompts instead of using a mouse. Command-line is different from GUI OS, i.e., Graphical User Interface Operating System. It only needs a keyboard to control and navigate by passing the commands instead of a mouse.
For example, to open the windows folder in the windows command line, we use “C:\Windows”.
However, using the command line requires one to remember lots of commands. However, it is very useful which should not be ignored. Command-line allows a user to perform all the tasks that they can perform on a GUI OS. And sometimes, there are many tasks that we can accomplish even quicker than GUI OS, and they are very easy to automate as well.
For example, if a person is asked to rename 50 files in a folder at a time. So, setting an environment variable with GUI can be very time-consuming but with the help of the windows command line, this can be done in seconds by passing a single command using a batch file or other scripts.
What is an Environment Variable?
An environment variable is a dynamic object that contains an editable value. It can be used by more than one software program in windows. The role of environment variables is to let the programs know which file directories to install, the location of temporary files, and the user profile settings. These are named environment variables because they shape the environment of the essential programs to run a computer.
How to set environment variables?
To set up an environment variable, the running system should have windows 10 OS. The user account should have admin privilege. It should also have access to Command Prompt or Windows PowerShell. Users can set up environment variables in two ways –
1. Via GUI
To set up environment variable using GUI, the user should follow the following steps-
- With the help of windows run, open System Properties.
- Then select the Advanced option in the top menu and set Environment Variables.
- The Environment Variables have two sections, user-specific and system-wide. So, to add a new environment variable, press the new button. Then, the user can add the variable according to their choice.
2. Via Command Prompt
- To set an environment variable with the help of a command prompt, use ‘setx’.
- For example – setx [variable_name] “[variable_value]. Here, the variable name can be assigned the name of the variable you want to keep and value, the value you want to assign the variable.
- Furthermore, if a user wants to add a system-wide environment variable, then use – setx [variable_name] “[variable_value]” /M.
Other useful articles:
- Basic Windows CMD commands
- Cool CMD Commands Tips and Tricks
- Best CMD Commands for Hacking
- CMD Commands for Wireless Network Speed
- Useful Keyboard Shortcuts for CMD
- What Info about My Laptop Can I Check with CMD and How?
- Getting Started with CMD Windows
- TOP-12 Command-Line Interview Questions (Basic)
- Command-Line Interview Questions (Advanced)
- CMD Commands to Repair Windows
- CMD Commands to Speed Up Computer
- CMD Commands for MAC OS
- How Does the Command Line Work?
- MS-Dos Interview Questions in 2021
- Windows OS Versions and History
- Recent Windows Versions Compared
- Basic Windows Prompt Commands for Every Day
- Windows Command Line Cheat Sheet For Everyone
- Windows Command Line Restart
- Windows Command Line for Loop
- Windows Command — Change Directory
- Windows Command — Delete Directory
- Windows Command Line – Set Environment Variable