在powershell中打印环境变量时如何不修剪线条?

问题描述

根据this,我可以在 PowerShell 中打印环境:

dir env:

所以我做了,例如对于 Path 和其他比我看到的窗口更长的环境变量:

 Path                      C:\python39\Scripts\;C:\python39\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;...

我不想要...,我想看到所有变量的环境变量值的全长。

我尝试阅读:https://superuser.com/questions/1049531/how-to-fix-truncated-powershell-output-even-when-ive-specified-width-300并尝试cd env: -Width 1000,或者尝试cd env:然后Get-ChildItem -Width 1000,它不起作用,并搜索了谷歌但没有成功。 dir env: | Out-String -width 999 导致无数的空行。有效的是dir env: | cat,但随后变量的名称消失了。

有什么办法可以查看所有带有变量名的环境变量的未截断值?

解决方法

从字面上理解您的问题,如果您只想在控制台上查看 env 变量及其未截断的值,您可以简单地将输出格式化为列表:

Get-ChildItem env: | Format-List

这将按以下方式显示信息,如果值超过可用空间,则插入可视换行符:

Name  : PSModulePath
Value : C:\Users\{and so on}
,

out-string 在 Windows PowerShell 和 PowerShell Core 上的行为略有不同 - 看起来 PowerShell Core 使用 -Width 作为最大宽度,而 Windows PowerShell pads 每行到指定的宽度,因此您会看到每个环境变量都由 lot 空格分隔。

PowerShell 核心

PS 7.1.3> dir env: | out-string -width 9999

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\Mike\AppData\Roaming
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files

Windows PowerShell

PS 5.1> dir env: | out-string -width 9999

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData




APPDATA                        C:\Users\Mike\AppData\Roaming




CommonProgramFiles             C:\Program Files\Common Files




CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files




CommonProgramW6432             C:\Program Files\Common Files

(填充不按比例缩放!)

这有点难看,但如果您想让 Windows PowerShell 输出看起来相同,您可以执行以下操作:

PS> ((dir env: | format-table | out-string -width 9999) -split [System.Environment]::NewLine).Trim() -join [System.Environment]::NewLine

基本上,将输出分成几行,修剪它们,然后再次将它们连接在一起。