Environment variables in an operating system are values that contain information about the system environment, and the currently logged in user. They existed in OSes before Windows as well, such as MS-DOS. Applications or services can use the information defined by environment variables to determine various things about the OS, for example, to detect the number of processes, the currently logged in user's name, the folder path to the current user's profile or the temporary files directory. Today, we will review a number of methods you can use to create a new user and system environment variable in Windows 10.
Windows 10 has several types of environment variables: user variables, system variables, process variables and volatile variables. User environment variables are accessible to all apps which run in the current user context, system environment variables apply to all users and processes on the PC; process variables are applicable only to a specific process and volatile variables are those which exist only for the current logon session. Most interesting of these are user, system and process variables, as we can modify them.
Example: A user environment variable.
Example: A system environment variable.
Windows 10 stores user environment variables under the following Registry key:
HKEY_CURRENT_USER\Environment
System variables are stored under the following key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Reference: How to see names and values of environment variables in Windows 10
To Create a User Environment Variable in Windows 10,
- Open the classic Control Panel.
- Navigate to Control Panel\User Accounts\User Accounts.
- On the left, click on the Change my environment variables link.
- In the next dialog, click the New button under the User variables for <username> section.
- Enter a variable name you want to create, then enter a variable value you want to assign to it. The dialog allows browsing for a file or folder to save your time.
- Click the OK button, and you are done.
Note: Re-open the required apps (e.g. Command Prompt) to make them read your new environment variable.
Tip: There are a number of other methods you can use to open the environment variables editor in Windows 10. First of all, you can create a special shortcut to open it directly. See Create Environment Variables Shortcut in Windows 10.
Also, there's a special RunDLL command that you can use (Press Win + R and copy-paste it to the Run box):
rundll32.exe sysdm.cpl,EditEnvironmentVariables
Finally, you can right-click the This PC icon in File Explorer and select Properties from the context menu. Click the "Advanced System Settings" link on the left. In the next dialog, "System Properties", you will see the Environment Variables... button in the bottom of the Advanced tab. Moreover, the Advanced System Settings dialog can be directly opened with the systempropertiesadvanced
command entered into the Run dialog.
Create a User Environment Variable in Command Prompt
- Open a new command prompt
- Type the following command:
setx <variable_name> "<variable_value>"
- Substitute
<variable_name>
with the actual name of the variable you want to create. - Substitute
"<variable_value>"
with the value you want to assign to your variable.
Do not forget to restart your apps (e.g. Command Prompt) to make them read your new environment variable.
The setx command is a console tool that can be used to set or unset user and system environment variables. In the general case, the syntax is as follows:
setx variable_name variable_value
- set an environment variable for the current user.
setx /M variable_name variable_value
- set an environment variable for all user (system-wide).
Type setx /? in a command prompt to see more details about this tool.
Create a User Environment Variable in PowerShell
- Open PowerShell.
- Type the following command:
[Environment]::SetEnvironmentVariable("<variable_name>", "<variable_value>" ,"User")
- Substitute
<variable_name>
with the actual name of the variable you want to create. - Substitute
"<variable_value>"
with the value you want to assign to your variable.
Similarly, you can create a system environment variable.
Create a System Environment Variable
- Open the Run dialog (Win + R), and execute the command
systempropertiesadvanced
. - In the System Properties dialog, switch to the Advanced tab. Click on the Environment Variables... button.
- In the next dialog, click the New button under the System variables section.
- Set the desired name for a variable you want to create, and specify its value, then click OK.
Create a User Environment Variable in Command Prompt
- Open a new command prompt as Administrator.
- Type the following command:
setx /M <variable_name> "<variable_value>"
- Substitute
<variable_name>
with the actual name of the variable you want to create. - Substitute
"<variable_value>"
with the value you want to assign to your variable.
The /M switch makes the setx command create a system variable.
Create a System Environment Variable in PowerShell
- Open PowerShell as Administrator. Tip: You can add "Open PowerShell As Administrator" context menu.
- Type the following command:
[Environment]::SetEnvironmentVariable("<variable_name>", "<variable_value>" ,"Machine")
- Substitute
<variable_name>
with the actual name of the variable you want to create. - Substitute
"<variable_value>"
with the value you want to assign to your variable.
The last parameter of the SetEnvironmentVariable call tells it to register the given variable as a system variable.
That's it.
Related articles:
- Create Environment Variables Shortcut in Windows 10
- How to see names and values of environment variables in Windows 10
- See names and values of environment variables for a process in Windows 10
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
Fix typo pplications.
Thank you.