PowerShell: View/Set Environment Variables

By Xah Lee. Date: . Last updated: .

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. [Windows: Registry Tutorial] When PowerShell starts, it reads the Registry and export them into current session of PowerShell.

Environment Variable in Current Session

List Environment Variables

List environment variables:

dir env:

Show env vars whose name contains “path”:

dir env:*path*

Show value of “path”:

$env:path -split ";"

Set Environment Variable

Sets a env var named abc for current session:

$env:abc = "alice"

Get value of a env var:


Remove Environment Variable

Deleting a env var from the current session:

rm env:abc
# adding path to the path env var in current session
$env:path = $env:path + ";C:\Program Files (x86)\ErgoEmacs\hunspell"

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 env: provider.

To manipulate env var in the registry, use the .NET object like the following:


Show env var named “xyz” of the category “User”:

[environment]::GetEnvironmentVariable("xyz", "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

Examples of creating/setting a environment variable:

[Environment]::SetEnvironmentVariable("xx", "something", "User")
[Environment]::SetEnvironmentVariable("POWERSHELL_TELEMETRY_OPTOUT", "1", "User")
[Environment]::SetEnvironmentVariable("DOTNET_CLI_TELEMETRY_OPTOUT", "1", "User")
[Environment]::SetEnvironmentVariable("POWERSHELL_TELEMETRY_OPTOUT", "1", "Machine")
[Environment]::SetEnvironmentVariable("DOTNET_CLI_TELEMETRY_OPTOUT", "1", "Machine")
[Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Program Files (x86)\PHP", "User")

The syntax [Environment]::SetEnvironmentVariable means calling the .NET object “Environment” and using its method “SetEnvironmentVariable”.

note: all arg should be string. Setting to "Machine" need admin.


# removing a env var from registry
[Environment]::SetEnvironmentVariable("xyz", $null, "User")


If you want PowerShell to update its env var session from the registry, exit PowerShell and start a new session.


powershell env var 2021-01-24 PowerShell path env 2021-03-05




Script Examples