PowerShell: Environment Variable in Registry
💡 TIP: Windows environment variable names are case-insensitive.
Persistent Environment Variable
- Persistent environment variables are stored in Windows Registry. 〔see PowerShell: Windows Registry〕
- When PowerShell launches, it reads the registry to get the env vars for the current session.
View Persistent 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"
-
Persistent environment variable for current user. Stored in Registry.
"Machine"
-
Persistent 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.