![](/img/trans.png)
[英]Powershell, retrieve columns width from a previous Format-Table -Autosize
[英]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
但如果文件名字符很大,分辨率列信息就会丢失。 名称列信息重叠。
但我不确定如何实现属性宽度或自动大小。
您可以使用带有计算属性的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.