Display all environment variables from a running PowerShell script

PowershellEnvironment Variables

Powershell Problem Overview


I need to display all configured environment variables in a PowerShell script at runtime. Normally when displaying environment variables I can just use one of the following at the shell (among other techniques, but these are simple):

gci env:*
ls Env:

However, I have a script being called from another program, and when I use one of the above calls in the script, instead of being presented with environment variables and their values, I instead get a list of System.Collections.DictionaryEntry types instead of the variables and their values. Inside of a PowerShell script, how can I display all environment variables?

Powershell Solutions


Solution 1 - Powershell

Shorter version:

gci env:* | sort-object name

This will display both the name and value.

Solution 2 - Powershell

Shortest version (with variables sorted by name):

gci env:

Solution 3 - Powershell

I finally fumbled my way into a solution by iterating over each entry in the dictionary:

(gci env:*).GetEnumerator() | Sort-Object Name | Out-String

Solution 4 - Powershell

> Short version with a wild card filter:

gci env: | where name -like 'Pro*'

Solution 5 - Powershell

I don't think any of the answers provided are related to the question. The OP is getting the list of Object Types (which are the same for each member) and not the actual variable names and values. This is what you are after:

gci env:* | select Name,Value

Short for:

Get-ChildItem Env:* | Select-Object -Property Name,Value

Solution 6 - Powershell

This command works also:

dir env:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBender the GreatestView Question on Stackoverflow
Solution 1 - PowershelljaymjarriView Answer on Stackoverflow
Solution 2 - PowershellVlad RudenkoView Answer on Stackoverflow
Solution 3 - PowershellBender the GreatestView Answer on Stackoverflow
Solution 4 - PowershellEmilView Answer on Stackoverflow
Solution 5 - PowershellMauricio_BRView Answer on Stackoverflow
Solution 6 - PowershellChristopher ScottView Answer on Stackoverflow