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)
这将显示名称和值.
小智 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)
带有通配符过滤器的简短版本:
gci env: | where name -like 'Pro*'
Run Code Online (Sandbox Code Playgroud)
默认情况下,长环境变量值会被截断。
这是获取具有完整值的环境变量排序列表的一种快速方法:
Get-ChildItem env:* | Sort-Object Name | Format-List
Run Code Online (Sandbox Code Playgroud)
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.
To list environment-variable names only:
gci env: -Name
# Alternative, using property access:
(gci env:).Name
Run Code Online (Sandbox Code Playgroud)
To get a specific environment variable's value, e.g. the value of USERNAME, it's easiest to use namespace variable notation:
# Output the value of environment variable "USERNAME"
$env:USERNAME
# Alternative, using gc (alias of Get-Content)
# Needed if the name is stored in a variable.
gc env:USERNAME
Run Code Online (Sandbox Code Playgroud)
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)
| 归档时间: |
|
| 查看次数: |
56199 次 |
| 最近记录: |