151

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Bender the Greatest
  • 17,062
  • 18
  • 82
  • 145

7 Answers7

211

Shorter version:

gci env:* | sort-object name

This will display both the name and value.

Jim Aho
  • 7,796
  • 11
  • 49
  • 80
jaymjarri
  • 2,464
  • 1
  • 17
  • 7
  • 1
    Could you explain why it behaves like that and how your command fixes it, please? – David Ferenczy Rogožan Sep 06 '18 at 12:43
  • 11
    That's the sign of progress because `env` was too easy. Damn you M$ people. – matcheek Apr 03 '19 at 07:19
  • I want to add that this behavior was something I encountered in PowerShell 4, but as of 5.1+ I can use the variants shown in the question within a script and expect it to display the variable name and value. Note that `gci env:` will now sort the variables by `Name`. though `gci env:*` does *not*. – Bender the Greatest Sep 10 '21 at 17:35
59

Shortest version (with variables sorted by name):

gci env:
Vlad Rudenko
  • 1,873
  • 1
  • 20
  • 22
16

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

(gci env:*).GetEnumerator() | Sort-Object Name | Out-String
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Bender the Greatest
  • 17,062
  • 18
  • 82
  • 145
  • doesn't run on Linux for me. Are you missing `gci` to get the child items? – Thufir Jan 22 '20 at 22:16
  • I haven't tried this on powershell core, but gci is in my answer. Note that this question is for [powershell] and not [powershell-core], so solutions may not work for the latter. – Bender the Greatest Jan 23 '20 at 01:30
12

Short version with a wild card filter:

gci env: | where name -like 'Pro*'
Emil
  • 1,918
  • 2
  • 22
  • 23
6

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
Mauricio_BR
  • 61
  • 1
  • 1
2

This command works also:

dir env:
Christopher Scott
  • 2,013
  • 1
  • 20
  • 23
-2

If you're using PowerShellCore, you can also use ls env:

WeihanLi
  • 57
  • 7
  • What is `list`? I see no such command in PowerShell (Core) v7.2.4. Did you mean `ls`? If so, that's [yet another alias for `Get-ChildItem`](https://stackoverflow.com/questions/39800481/display-all-environment-variables-from-a-running-powershell-script#comment122736361_69407908) and also already stated in the question. – Lance U. Matthews May 22 '22 at 03:22
  • Yeah, it should be `ls`, sorry for the miss-leading, thanks for your correction @LanceU.Matthews – WeihanLi May 22 '22 at 13:42