IntroductionEnvironment 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.
- A system running Windows 10
- User account with admin privileges
- Access to the Command Prompt or Windows PowerShell
Check Current Environment VariablesThe method for checking current environment variables depends on whether you are using the Command Prompt or Windows PowerShell:
List All Environment VariablesIn the Command Prompt, use the following command to list all environment variables:
If you are using Windows PowerShell, list all the environment variables with:
Check A Specific Environment VariableBoth the Command Prompt and PowerShell use the
echocommand to list specific environment variables.The Command prompt uses the following syntax:
In Windows PowerShell, use:
[variable_name]is the name of the environment variable you want to check.
Set Environment Variable in Windows via GUIFollow 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 PromptUse the
setxcommand to set a new user-specific environment variable via the Command Prompt:
setx [variable_name] "[variable_value]"
[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.
setx Test_variable "Variable value"
To add a system-wide environment variable, open the Command Prompt as administrator and use:
Note: You need to restart the Command Prompt for the changes to take effect.
setx [variable_name] "[variable_value]" /M
Unset Environment VariablesThere are two ways to unset environment variables in Windows:
Unset Environment Variables in Windows via GUITo 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 RegistryWhen 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
regcommand allows you to review and unset environment variables directly in the registry.
Use the following command to list all user-specific environment variables:
regcommand works the same in the Command Prompt and Windows PowerShell.
List all the system environment variables with:
reg query HKEY_CURRENT_USEREnvironment
If you want to list a specific variable, use:
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment"
reg query HKEY_CURRENT_USEREnvironment /v [variable_name]
reg query "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name]
/v: Declares the intent to list a specific variable.
[variable_name]: The name of the environment variable you want to list.
reg delete HKEY_CURRENT_USEREnvironment /v [variable_name] /f
reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /v [variable_name] /f
/fparameter is used to confirm the
reg deletecommand. Without it, entering the command triggers the
Delete the registry value EXAMPLE (Yes/No)?prompt.
setxcommand 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
setxcommand, set a throwaway variable. For example:
setx [variable_name] trash