Xah Lee, 2009-07
If you are not familiar with Windows env var, how to set or view them with GUI, see: Windows Environment Variables.
The following discusses how to use PowerShell to manipulate env vars.
# show current env vars Get-ChildItem Env: # show env vars whose name contains “path” Get-ChildItem Env:*path* # note: Get-ChildItem has alias “dir”
# sets a env var named myX for current session $env:myX = "la la la" # get value of a env var $env:myX # deleting a env var from the current session Remove-Item env:myX
# adding to a path to the path env var $env:path = $env:path + ";C:\Program Files (x86)\ErgoEmacs\hunspell"
Permanent env vars are 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 for permanent use, use the .NET object like the following:
# creates “myY” of category “User”, and set the value to “"la la"” [Environment]::SetEnvironmentVariable("myY", "la la", "User")
The syntax “[Environment]::SetEnvironmentVariable” means calling the .NET object “Environment” and using its method “SetEnvironmentVariable”.
# displaying a env var named “myY” of the category “User”. [environment]::GetEnvironmentVariable("myY", "User")
The possible values for the second argument in GetEnvironmentVariable are: “"Process"”, “"User"”, “"Machine"”.
# removing a env var from registry [Environment]::SetEnvironmentVariable("myY", $null, "User")
Note: If you want PowerShell to update its env var session from the registry, you can restart PowerShell.
get-help environment_variables, get-help about_provider.
Related essays: