How To Set Environment Variable For Windows?

The environment variables in Windows are very popular in order to pass some basic information or data into different tools, applications, services, frameworks In this tutorial we will examine how to set or create a new environment variable 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.

Open System Properties From Windows Run

From the System Properties screen click on the Advanced tab where you will see the Environment Variables button. Click on the Environment Variables button.

Open Environment Variables From System Properties

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.

Environment Variables

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.

New User Variable

The newly added variable will be listed in the related User or System variables pane like below.

List Environment Variables

Set Environment Variable with Set-Variable PowerShell Command

The PowerShell provides 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 variable for PowerShell. The -Name attribute 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.

What Is Windows Command Prompt?

We will use the echo command which will print given environment variable to the screen. The environment variable MYVAR can be accessed as %MYVAR% like below.

> 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 like below.

PS C:\> Get-Variable

The output is like below.

Check and Use Environment Variable with Get-Variable PowerShell Command

Also 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"
READ  net use /delete - Delete Windows Shares

Leave a Comment