You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中Format-Table如何增加列间距?

为Format-Table自定义列间距的解决方法

问题场景

处理System.IO.FileInfo数组时,使用Format-Table自定义输出格式,需要展示文件名、最后写入时间和文件大小。现有代码如下:

$queue = Find-FilesToUpload -server $server | Sort-Object -property lastWriteTime | `
   Format-Table -property name, lastWriteTime, 
   @{name = "Bytes"; expression = {$_.length}; alignment="right"; width=15; format="n0"} | `
   Out-Host -paging

当前输出列间距过近,阅读不便:

Name         LastWriteTime          Bytes
----         -------------          -----
pierson.ogg  8/23/2023 2:06:08 PM 460,664
ohiamsoh.htm 8/23/2023 2:06:55 PM   3,501

希望实现列间距更宽松的输出:

Name            LastWriteTime             Bytes
----            -------------             -----
pierson.ogg     8/23/2023 2:06:08 PM    460,664
ohiamsoh.htm    8/23/2023 2:06:55 PM      3,501

解决方法

Format-Table没有直接设置列间距的参数,但可以通过为每一列明确指定宽度来拉开间距。原来的内置列(Name、LastWriteTime)未指定宽度,PowerShell会自动压缩到最小必要宽度,导致列间距狭窄。修改代码,给所有列添加自定义宽度配置:

$queue = Find-FilesToUpload -server $server | Sort-Object -property lastWriteTime | `
   Format-Table -property `
      @{name = "Name"; expression = {$_.Name}; width=20}, `
      @{name = "LastWriteTime"; expression = {$_.LastWriteTime}; width=25}, `
      @{name = "Bytes"; expression = {$_.Length}; alignment="right"; width=15; format="n0"} | `
   Out-Host -paging

说明

  • 对每个列(包括原本的内置属性)都使用哈希表定义格式,指定足够的width值,让列拥有冗余宽度,自然形成宽松的列间距。
  • 可以根据实际需求调整width的数值,比如文件名较长时可以增大Name列的宽度,保证内容不被截断。

内容的提问来源于stack exchange,提问作者aksarben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 09:27:34