簡體   English   中英

Powershell 調整格式表格寬度

[英]Powershell adjust the format table width

這是腳本

get-childitem D:\media\*.mkv -recurse | Select-Object -Property name,  @{n='Resolution';expression={mediainfo $_.FullName --inform="Video;%Width%x%Height%"}}

output 是

Name                      Resolution
----                      ----------
Video File -Copy (2).mkv 1920x1080 
Video File -Copy (3).mkv 1920x1080 
Video File -Copy (4).mkv 1920x1080 
Video File -Copy (5).mkv 1920x1080 
Video File -Copy (6).mkv 1920x1080 
Video File -Copy (7).mkv 1920x1080 
Video File -Copy.mkv     1920x1080 
Video File.mkv           1920x1080 

但如果文件名字符很大,分辨率列信息就會丟失。 名稱列信息重疊。

我讀了這個頁面https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-table?view=powershell-7.2

但我不確定如何實現屬性寬度或自動大小。

您可以使用帶有計算屬性Format-Table來限制Name列的寬度; 例如:

... | Format-Table @{ Name='Name'; Expression = 'Name'; Width = 60 }, Resolution

然后在 Windows PowerShell / PowerShell (Core) 7+ 中用... / 截斷較長的值。 您可以添加-Wrap以完整顯示它們,並根據需要分布在任意多行中。

替代方案 - 不會在屏幕上很好地呈現 - 是 pipe 到Out-String並使用其-Width參數和一個足以容納所有表列的終端列數:

... | Out-String -Width 300

注意:對於您的 output 對象, Format-Table格式是隱含的(它們具有 4 個或更少的屬性),這就是為什么上面沒有明確調用它的原因。

作為旁白:

  • Format-Table -Autosize沒有幫助,因為它不能確保顯示所有列 - 請參閱此答案以獲取更多信息。

示例代碼:

[pscustomobject] @{
  Name = 'x' * 80 + 'y'
  Resolution = '1920x1080'
},
[pscustomobject] @{
  Name = 'x' * 280 + 'z'
  Resolution = '2560x1600'
} |
  Format-Table -Wrap @{ Name='Name'; Expression = 'Name'; Width = 60 }, Resolution

Output:

Name                                                         Resolution
----                                                         ----------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1920x1080
xxxxxxxxxxxxxxxxxxxxy
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2560x1600
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM