The environment variables in Windows are very popular in order to pass some basic information or data into different tools, applications, services, and frameworks In this tutorial we will examine how to set or create a new environment variable in Windows. These instructions can be implemented in Windows XP, Windows 7, Windows 10, Windows Server, Linux, Ubuntu, Debian, Mint, Kali, CentOS, Fedora, RHEL, etc.
Set Environment Variable From GUI
Windows operating system provides different ways to set environment variables. The most known way is to use the “Environment Variables” tool to set from GUI. In order to open the Environment Variables screen follow these steps. Open the Windows Run by pressing WIN+R keys and then type sysdm.cpl and click OK.
From the System Properties screen click on the Advanced tab where you will see the Environment Variables button. Click on the Environment Variables button.
The Environment Variables screen will be displayed like below. As we can see that there are two types of variables called User variables and System variables. User variables are only used by the current user. System variables are used by all users.
We will click on the New button according to our case where for this example we will create a new user variable. The following New User Variable dialog box contains the Variable name and Variable value box where we will put this information. The last step is clicking on the OK button which will save and set these new variables. The variable name can be uppercase or lowercase. The variable values can be words or strings or numbers. Alternatively, the variable name and variable value can be loaded via the
Browse File... .
The newly added variable will be listed in the related User or System variables pane like below.
Set Environment Variable with Set-Variable PowerShell Command
PowerShell provides a different environment than MS-DOS but the MS-DOS variables can be also used in PowerShell. The Set-Variable command can be used to set new environment variables for PowerShell. The -Name attribute is used to set variable name and -Value attribute is used to set variable value.
PS C:\> Set-Variable -Name "MYVAR" -Value "This is my variable"
Check and Use Environment Variable From Command Line
Now we know that the environment variable is added but we can also use the command-line interface to check the new environment variable. The command prompt or MS-DOS can be used to check the new environment variable. First, open the MS-DOS which is explained below.
We will use the echo command which will print the given environment variable to the screen. The environment variable MYVAR can be accessed as %MYVAR% like below. The echo command can be used in MS-DOS or PowerShell without a problem.
> echo %MYVAR%
Check and Use Environment Variable with Get-Variable PowerShell Command
The Get-Variable command can be used to list PowerShell variables. If no parameter is provided all defined variables are listed below.
PS C:\> Get-Variable
The output is like below.
Also, a specific variable and its value can be listed by using the
-Name attribute and providing the variable name. In the following example, we will display the MYVAR variable value.
PS C:\> Get-Variable -Name "MYVAR"