PowerShell: Environment Variable in Registry

By Xah Lee. Date: .

💡 TIP: Windows environment variable names are case-insensitive.

Permament Environment Variable

View Permament Environment Variable

# show env var named path of the category User
[environment]::GetEnvironmentVariable("path", "User")

The possible values for the second argument in GetEnvironmentVariable are:

"Process"

Current session of PowerShell.

"User"

Permanent, environment variable for current user. Stored in Registry.

"Machine"

Permanent, environment variable for this machine. Stored in Registry. Require admin privilege.

Create/Set environment variable in Registry

# create or set a environment variable in registry of category User
[Environment]::SetEnvironmentVariable("xx", "123", "User")
# create or set a environment variable in in registry of category Machine
[Environment]::SetEnvironmentVariable("xx", "123", "Machine")

Remove environment variable in Registry

# remove a env var from registry
[Environment]::SetEnvironmentVariable("xx", $null, "User")
# or
[Environment]::SetEnvironmentVariable("xx", $null, "Machine")

Refresh

Changes of environment variable in registry is not reflected in current PowerShell session. Type exit to exit PowerShell, then start a new session.

PowerShell: Environment Variable