显示正在运行的PowerShell脚本中的所有环境变量

Ben*_*est 81 powershell environment-variables

我需要在运行时在PowerShell脚本中显示所有已配置的环境变量.通常在显示环境变量时,我可以在shell中使用以下其中一种(除了其他技术,但这些很简单):

gci env:*
ls Env:
Run Code Online (Sandbox Code Playgroud)

但是,我从另一个程序调用了一个脚本,当我在脚本中使用上述调用之一时,而不是显示环境变量及其值,而是获取System.Collections.DictionaryEntry类型列表而不是变量及其值.在PowerShell脚本中,如何显示所有环境变量?

jmj*_*rri 122

更短的版本:

gci env:* | sort-object name
Run Code Online (Sandbox Code Playgroud)

这将显示名称和值.

  • 这是进步的标志,因为 `env` 太容易了。该死的 M$ 人。 (11认同)
  • 我想补充一点,这种行为是我在 PowerShell 4 中遇到的,但从 5.1+ 开始,我可以在脚本中使用问题中显示的变体,并期望它显示变量名称和值。请注意,“gci env:”现在将按“Name”对变量进行排序。虽然 `gci env:*` 不*。 (2认同)
  • 我假设_shorter_你的意思是相对于[OP自己的答案](/sf/answers/2786042451/)。然而,对于所提出的问题,省略“Out-String”至关重要。如果您只想以良好的格式列出 PowerShell 中的所有环境变量(包括它们的值),“gci env:”即可(不需要“*”,不需要“Sort-Object”,因为输出中没有“*”自动按名称排序)。 (2认同)

Vla*_*nko 24

最短版本(变量按名称排序):

gci env:
Run Code Online (Sandbox Code Playgroud)


小智 15

我认为所提供的任何答案都与该问题无关。OP 正在获取对象类型列表(每个成员都相同),而不是实际的变量名称和值。这就是你所追求的:

gci env:* | select Name,Value
Run Code Online (Sandbox Code Playgroud)

短缺:

Get-ChildItem Env:* | Select-Object -Property Name,Value
Run Code Online (Sandbox Code Playgroud)


Ben*_*est 13

我终于通过遍历字典中的每个条目来摸索我的方式:

(gci env:*).GetEnumerator() | Sort-Object Name | Out-String
Run Code Online (Sandbox Code Playgroud)


Chr*_*ott 12

该命令也有效:

dir env:
Run Code Online (Sandbox Code Playgroud)

  • `dir` 和 `gci` 都是 `Get-ChildItem` 的别名 (11认同)

Emi*_*mil 9

带有通配符过滤器的简短版本:

gci env: | where name -like 'Pro*'
Run Code Online (Sandbox Code Playgroud)

  • 您还可以跳过管道,只需将通配符过滤器与“Get-ChildItem”一起使用。例如:`gci env:Pro*` (3认同)

GaT*_*mas 7

默认情况下,长环境变量值会被截断。

这是获取具有完整值的环境变量排序列表的一种快速方法:

Get-ChildItem env:* | Sort-Object Name | Format-List
Run Code Online (Sandbox Code Playgroud)


mkl*_*nt0 6

tl;dr

Since you were looking for a friendly string representation of the environment-variable name-value pairs:

gci env: | oss
Run Code Online (Sandbox Code Playgroud)

oss is a built-in wrapper function for Out-String -Stream, and therefore returns each name-value pair as its own string; pipe to Out-String (without -Stream) to get a single, multi-line string (albeit one that invariably and unexpectedly has a trailing newline - see GitHub issue #14444).


To list the names and values of all environment variables in PowerShell, sorted by name,[1] list the content (child items) of the env: PowerShell drive using the Get-ChildItem cmdlet (a built-in alias of which is gci):

# 'gci' is a built-in alias of the 'Get-ChildItem' cmdlet.
# Avoid alias 'ls', because on Unix-like platforms 
# it isn't defined and instead refers to the standard utility of that name.
# The output is implicitly *sorted by variable name*.
gci env:

# Use *wildcards* to list variables by *name pattern*; e.g, all whose
# name starts with "home"
gci env:home*
Run Code Online (Sandbox Code Playgroud)

The above outputs objects, namely instances of [System.Collections.DictionaryEntry] describing each variable as a name-value pair, with .Key (.Name) and .Value properties. PowerShell's for-display formatting system automatically renders these in a friendly two-column format.

If you stringify these objects with (potentially implied) .ToString():

  • In Windows PowerShell, they uselessly stringify as their type name, i.e. as verbatim 'System.Collections.DictionaryEntry'

  • In PowerShell (Core) 7, they now more meaningfully stringify as '[<name>, <value>]'

  • Try with (% is a built-in alias of the ForEach-Object cmdlet):

    gci env: | % tostring
    
    # Ditto with Write-Host, which also uses .ToString() stringification
    gci env: | Write-Host
    
    Run Code Online (Sandbox Code Playgroud)

If you want to stringify them as they would print to the display, using the friendly two-column format, use the Out-String cmdlet:

# Outputs *friendly* string representations
gci env: | oss # 'oss' is a built-in wrapper function for 'Out-String -Stream'
Run Code Online (Sandbox Code Playgroud)

Note: If you use Out-String without -Stream, you get a single, multi-line string as the output, though note that it will have a trailing newline.[2]


[1] Note that using Get-ChildItem / gci with env:*, i.e. wildcard character * following the drive specification env: - is not only unnecessary for getting all variables, it actually results in unsorted output.

[2] That a trailing newline is invariably appended is problematic, as discussed in GitHub issue #14444


小智 5

Powershell中有多种获取所有环境变量的方法

 [System.Environment]::GetEnvironmentVariables()
 or
 dir env:
Run Code Online (Sandbox Code Playgroud)

通过名称获取环境变量

[System.Environment]::GetEnvironmentVariable("USERNAME")
 $env:USERNAME
Run Code Online (Sandbox Code Playgroud)