Table of Contents
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 post, we will show you how to set an environment variable in Windows from the command-line prompt (CMD) and from the Windows PowerShell. In the examples below we will set an environment variable temporary (for the current terminal session only), permanently for the current user and globally for the whole system.
Environment variable scopes
In Windows, there’re three scopes to set an environment variable:
- Session scope: The environment variables valid in the current session only. Closing the PowerShell window will revert the environment variables to its pre-determined state
- User scope: User environment variables are specific only to the currently logged-in user.
- System scope: System environment variables are globally accessed by all users.
From Windows, you can add an environment variable with GUI from Control Panel. But this post will focus on command line for automation and scripting.
Set Environment Variable in Windows Using PowerShell
In PowerShell we can get the list of environment variables using the Get-ChildItem cmdlet.
$Env:PATH -split ";"
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\admin\AppData\Roaming
ChocolateyInstall C:\ProgramData\chocolatey
ChocolateyLastPathUpdate 133396374426589550
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME DESKTOP-B2TOHJT
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
HOMEDRIVE C:
HOMEPATH \Users\admin
LOCALAPPDATA C:\Users\admin\AppData\Local
LOGONSERVER \\DESKTOP-B2TOHJT
NUMBER_OF_PROCESSORS 12
...
Set Environment Variable for the Current Session using PowerShell
To set an environment variable for the current terminal session. Use the below syntax:
$env:VAR_NAME="VALUE"
For example, we will create an environment variable named BACKUPREPO with is value is the path of the folder D:\Backup.
$env:BACKUPREPO="D:\Backup"
To get the value of an environment variable, use the following syntax $env:VAR_NAME.
Get the list of environment variables, you should see the newly created environment variable in the list.
PS D:\Backup> Get-ChildItem Env:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\mpnadmin\AppData\Roaming
BACKUPREPO D:\Backup
ChocolateyInstall C:\ProgramData\chocolatey
ChocolateyLastPathUpdate 133659380944392780
CLIENTNAME DESKTOP-B2TOHJT
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
...
From now, we can use the environment variable instead of the full path of the folder. For example, we can navigate to the backup repository using the environment variable.
Set Environment Variable Permanently using PowerShell
Using the $env:VAR_NAME will only temporarily add the environment variable into the current PowerShell session. Closing the PowerShell window will revert the environment variables to its pre-determined state. To permanently set an environment variable, we can use the following below methos.
You can use the below command to add an environment variable permanently with user or machine scope:
# Permanently set an environment variable for the current user:
# setx /M VAR_NAME "VALUE"
setx BACKUPREPO "D:\Backup"
# Permanently set global environment variable (for all users):
# setx /M VAR_NAME "VALUE"
setx /M BACKUPREPO "D:\Backup"
Close all running PowerShell windows then reopen a new one to verify it works.
Set Environment Variable in Windows Using CMD
In case you want to do it using CMD (Command Prompt):
Set an environment variable for the current terminal session:
set VAR_NAME="VALUE"
When you need to add an environment variable permanently using CMD. The command is the same as doing in PowerShell:
# Permanently set an environment variable for the current user:
# setx /M VAR_NAME "VALUE"
setx BACKUPREPO "D:\Backup"
# Permanently set global environment variable (for all users):
# setx /M VAR_NAME "VALUE"
setx /M BACKUPREPO "D:\Backup"