PowerShell: Set Environment Variable
This page is a tutorial on using PowerShell to view or set environment variables.
For introduction on Microsoft Windows environment variable, see: Windows Environment Variables Tutorial .
Windows environment variable names are case-insensitive.
Environment variable are stored in Windows Registery. [see Windows: Registry Tutorial] When PowerShell starts, it reads the Registry and export them into current session of PowerShell.
- Change to environment variable in current session is temporary. Once you exit shell, the changes are gone.
- Change to environment variable in the registry is permanent.
Adding Path to Path Environment Variable
Show the value of environment variable
$env:path -split ";"
suppose you want to add
[Environment]::SetEnvironmentVariable("path", $env:path + ";c:/Users/xah/bin/", "User")
restart PowerShell to see the new value.
Environment Variable in Current Session
List Environment Variables
List environment variables:
Show env vars whose name contains “path”:
Show value of “path”:
$env:path -split ";"
Set Environment Variable for current session
Sets a env var named abc for current session:
$env:abc = "alice"
Get value of a env var:
Remove Environment Variable for current session
Deleting a env var from the current session:
rmis alias of
Permament Environment Variable
Permanent environment variable is stored in Windows Registry.
When PowerShell launches, it reads the registry to get the env vars for the current session.
However, it does not update the registry whenever you create or remove a env var using the
To manipulate env var in the registry, use the .NET object like the following:
Show env var named “path” of the category “User”:
The possible values for the second argument in GetEnvironmentVariable are:
- Current session of PowerShell.
- Permanent, environment variable for current user. Stored in Registry.
- Permanent, environment variable for this machine. Stored in Registry. Require admin privilege.
Create/Set environment variable in Registry
Examples of creating/setting a environment variable:
[Environment]::SetEnvironmentVariable("xx", "something", "User")
[Environment]::SetEnvironmentVariable means calling the .NET object “Environment” and using its method “SetEnvironmentVariable”.
note: all arg should be string.
Remove environment variable in Registry
# removing a env var from registry [Environment]::SetEnvironmentVariable("xyz", $null, "User")
changes of environment variable in registry is not reflected in current PowerShell session. type
exit to exit PowerShell, then start a new session.