Windows environment variables are visible as a PowerShell drive (much like a file system drive) called Env:
To list all the environment variables use dir env: or in long format: Get-Childitem env:
Each environment variable is an object that can be retrieved with Get-Childitem (or Get-Item) and enumerated with Get-Member
Display the value of the COMPUTERNAME environment variable:
Get-Childitem env:computername
Display the values of all environment variables:
Get-Childitem env:
gci env: | sort name
Enumerate all the environment variables:
get-childitem -path env:* | get-member
This is easier if you first set-location (cd) to the Env: Drive
cd env:
Then to display a variable: get-childitem computername
The powershell expression parser can also handle environment variables directly using the syntax $env:variable-name
$env:computername
Store the 'temp' environment variable in a powershell (local) variable:
$result = Get-Item env:temp
In an expression use this syntax: $env:VariableName = "new-value"
For example, to append "finance" to the value of the TEAMS environment variable:
$env:TEAMS = $env:TEAMS + "finance"
To change environment variables use Set-Item, Remove-Item, and Copy-Item.
For example:
Set-Item -path env:TEAMS -value ($env:TEAMS + "finance")
Note the use of parentheses above.
When you change environment variables , the change affects only the current session, much like using the SET command in Windows. To make the changes permanent, you have to change them in the registry or with a utility like SETX. You must also have permission to change the values.
PowerShell Providers
In addition to environment variables, PowerShell providers also provide access to other data and components in a similar way - resembling a file system drive. This allows you to access many different types of data in a consistent way.
Built-in Providers
Alias - Windows PowerShell aliases {Alias}
Certificate - X509 certificates for digital signatures {cert}
Environment - Windows environment variables {Env}
FileSystem - File system drives, directories and files {filesystem}
Function - Windows PowerShell functions {Function}
Registry - Windows registry {HKLM, HKCU}
Variable - Windows PowerShell variables {Variable}
In powershell a fully qualified path name takes this form:
filesystem::c:\windows\system32\shell.dll
Additional providers may also be created and installed - Get-PsProvider will list them all.
Enumerate the properties of a provider with Get-psdrive: Get-psdrive Function | format-list *
e.g. to list all certificates use: Get-Childitem cert:
cd cert:
gci
“Most variables can show either an upward trend or a downward trend, depending on the base year chosen” - Thomas Sowell
Related:
Get-PSDrive - Get drive information (DriveInfo)
class: System.Collections.DictionaryEntry