怎样将Powershell的对象结果转换成文本并显示在控制台上。Powershell已经内置Out-Default命令追加在管道的命令串的末尾。因此你使用dir 和dir | out-default的结果是相同的。
Out-Default可以将对象转换成可视的文本。事实上Out-Default会首先调用Format-Table,将更多的属性默认隐藏。再调用Out-Host将结果输出在控制台上。因此下面的三组命令执行结果是相同的。

  1. ls
  2. ls | Format-Table | Out-Host
  3. ls | Out-Default

显示隐藏的对象属性

要查看对象结果的所有属性,可是使用

  1. ls | Format-Table *

这样因为属性和属性的内容太多可能不会显示完全,可以使用文本换行参数

  1. ls | Format-Table * -Wrap

格式化管道结果

首先可是使用下面的命令查看所有以Format打头的命令

  1. PS C:Powershell> Get-Command -Verb format
  2. CommandType Name Version Source
  3. ----------- ---- ------- ------
  4. Function Format-Hex 3.1.0.0 Microsoft.PowerShell.Utility
  5. Function Format-Volume 2.0.0.0 Storage
  6. Cmdlet Format-Custom 3.1.0.0 Microsoft.PowerShell.Utility
  7. Cmdlet Format-List 3.1.0.0 Microsoft.PowerShell.Utility
  8. Cmdlet Format-SecureBootUEFI 2.0.0.0 SecureBoot
  9. Cmdlet Format-Table 3.1.0.0 Microsoft.PowerShell.Utility
  10. Cmdlet Format-Wide 3.1.0.0 Microsoft.PowerShell.Utility

Format-Custom 使用自定义视图来设置输出的格式。
Format-List 将输出的格式设置为属性列表,其中每个属性均各占一行显示。
Format-Table 将输出的格式设置为表。
Format-Wide 将对象的格式设置为只能显示每个对象的一个属性的宽表。

显示指定的属性

要显示指定的属性,你首先得知道结果对象中的属性名,例如:

  1. PS C:Powershell> ls | Format-Table Name,Length,LastWriteTime
  2. Name Length LastWriteTime
  3. ---- ------ -------------
  4. ABC 2011/11/23 17:25:53
  5. myscript 2011/11/29 18:21:28
  6. a.html 67580 2011/11/24 18:30:13
  7. a.txt 26384 2011/11/24 20:04:31
  8. alias 12060 2011/11/24 20:26:36

使用通配符

例如要查看当前以i打头的进程,并显示进程的名字和其它以”pe”打头,以”64″结尾的进程。

  1. PS C:Powershell> Get-Process i* | Format-Table Name,pe*64
  2. Name PeakPagedMemorySize PeakWorkingSet64 PeakVirtualMemorySi
  3. 64 ze64
  4. ---- ------------------- ---------------- -------------------
  5. Idle 0 0 0
  6. IMECFMUI 946176 4292608 48054272
  7. IMECMNT 1564672 5320704 65482752
  8. IMEDICTUPDATE 1224704 4579328 31965184

脚本块作为属性

在Powershell中文件的Length默认以byte作为单位如果你象让它输出时以KB显示,可是考虑羡下面的方法。

  1. PS C:Powershell> ls | Format-Table Name,{ [int]($_.Length/1kb) }
  2. Name [int]($_.Length/1kb)
  3. ---- ----------------------
  4. function.ps1 21
  5. LogoTestConfig.xml 0
  6. ls.html 3
  7. name.html 7

修改列标题

使用合成的属性,如果使用脚本块作为标题,看着很不爽。可以使用Lable设置。同样是上面的例子,稍作修改。

  1. PS C:Powershell> $column = @{Expression={ [int]($_.Length/1KB) }; Label="KB" }
  2. PS C:Powershell> Dir | Format-Table Name, $column
  3. Name KB
  4. ---- ----------------------
  5. function.ps1 21
  6. LogoTestConfig.xml 0
  7. ls.html 3
  8. name.html

优化列宽度

因为Powershell的绝大多数输出都是实时的流模式,所以下一条结果的宽度未知,Powershell的结果会默认采用分散对齐,这样可以最大限度利用控制台的宽度,但是可以通过-auto参数对列的宽带进行优化,会将属性值的最大宽带作为每一列的宽度,对比一下吧:

  1. PS C:Powershell> ls
  2. 目录: C:Powershell
  3. Mode LastWriteTime Length Name
  4. ---- ------------- ------ ----
  5. d---- 2011/11/23 17:25 ABC
  6. d---- 2011/11/29 18:21 myscript
  7. -a--- 2011/11/24 18:30 67580 a.html
  8. -a--- 2011/11/24 20:04 26384 a.txt
  9. PS C:Powershell> ls | Format-Table -AutoSize
  10. 目录: C:Powershell
  11. Mode LastWriteTime Length Name
  12. ---- ------------- ------ ----
  13. d---- 2011/11/23 17:25 ABC
  14. d---- 2011/11/29 18:21 myscript
  15. -a--- 2011/11/24 18:30 67580 a.html

参考资料