使用格式命令更改输出视图Using Format Commands to Change Output View

    Format cmdlet 包括 Format-WideFormat-ListFormat-TableFormat-Custom在本用户指南中,我们将只介绍 Format-WideFormat-ListFormat-Table cmdlet。

    如果不指定要显示的特定属性,则每个 format cmdlet 都具有要使用的默认属性。各 cmdlet 也使用相同的参数名称,Property,来指定要显示的属性。因为 Format-Wide 只显示单个属性,其 Property 参数仅采用单个值,但 Format-ListFormat-Table 的属性参数接受一系列属性名称。

    如果你将命令 Get-Process-Name powershell 与 Windows PowerShell 运行的两个实例搭配使用,你将得到如下所示的输出:

    在本部分接下来的内容中,我们将探究如何使用 Format cmdlet 更改此命令的输出的显示方式。

    默认情况下,Format-Wide 只显示对象的默认属性。与每个对象关联的信息将显示在单个列中:

    1. powershell powershell

    你还可以指定非默认属性:

    1. PS> Get-Process -Name powershell | Format-Wide -Property Id
    2. 2760 3448

    使用列控制 Format-Wide 显示Controlling Format-Wide Display with Column

    1. Get-Command -Verb Format | Format-Wide -Property Noun -Column 1
    1. Custom
    2. Hex
    3. List
    4. Table
    5. Wide

    Format-List cmdlet 以列表的形式显示对象,同时标记每个属性并在单独的行上显示:

    你可以指定所需数目的属性:

    1. PS> Get-Process -Name powershell | Format-List -Property ProcessName,FileVersion
    2. ,StartTime,Id
    3. ProcessName : powershell
    4. FileVersion : 1.0.9567.1
    5. StartTime : 2006-05-24 13:42:00
    6. ProcessName : powershell
    7. FileVersion : 1.0.9567.1
    8. StartTime : 2006-05-24 13:54:28
    9. Id : 3448

    通过将 Format-List 与通配符搭配使用获取详细信息Getting Detailed Information by Using Format-List with Wildcards

    Format-List cmdlet 使你可以将通配符用作其 Property 参数的值。这样便可以显示详细信息。通常情况下,对象包含的信息比你需要的多,这就是默认情况下 Windows PowerShell 不显示所有属性值的原因。若要显示对象的全部属性,则使用 Format-List-Property * 命令。下面的命令针对单个进程生成超过 60 行的输出:

    虽然 Format-List 命令对显示详细信息很有用,但如果你想要包括多个项目的输出的概述,更常用的是一个更简单的表格视图。

    如果你在没有指定属性名称的情况下使用 Format-Table cmdlet 来格式化 Get-Process 命令的输出,你所得到的输出会和未执行任何格式设置时得到的完全一样。原因是进程通常以表格格式显示,和大多数 Windows PowerShell 对象一样。

    1. PS> Get-Process -Name powershell | Format-Table
    2. Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
    3. ------- ------ ----- ----- ----- ------ -- -----------
    4. 1488 9 31568 29460 152 3.53 2760 powershell
    5. 332 9 23140 632 141 1.06 3448 powershell

    改进 Format-Table 输出(自动调整大小)Improving Format-Table Output (AutoSize)

    尽管表格视图对显示大量可比较的信息很有用,但如果显示区域对于数据来说太窄,则可能导致数据难以理解。例如,如果你尝试显示进程路径、ID、名称和公司,你获得的却是进程路径和公司列的截断了的输出:

    1. PS> Get-Process -Name powershell | Format-Table -Property Path,Name,Id,Company
    2. Path Name Id Company
    3. ---- ---- -- -------
    4. C:\Program Files... powershell 2836 Microsoft Corpor...

    Format-Table cmdlet 仍可能会截断数据,但仅会在屏幕的末尾处进行截断。除最后一个显示的属性外,会让所有属性获得各自所需的大小,以使最长的数据元素得以正确显示。如果你交换属性值列表中路径公司的位置,你会发现公司名称是可见的,但路径则被截断:

    1. PS> Get-Process -Name powershell | Format-Table -Property Company,Name,Id,Path -
    2. AutoSize
    3. Company Name Id Path
    4. ------- ---- -- ----
    5. Microsoft Corporation powershell 2836 C:\Program Files\Windows PowerShell\v1...

    Format-Table 命令假定属性距离属性列表的开头越近,则该属性越重要。因此,它会尝试完整显示离列表开头最近的那些属性。如果 Format-Table 命令无法显示所有属性,它将从显示中删除某些列,并发出警告。如果你使名称变成列表中的最后一个属性,便可以看到这一行为:

    1. PS> Get-Process -Name powershell | Format-Table -Property Company,Path,Id,Name -
    2. AutoSize
    3. WARNING: column "Name" does not fit into the display and was removed.
    4. Company Path I
    5. d
    6. Microsoft Corporation C:\Program Files\Windows PowerShell\v1.0\powershell.exe 6

    在上面的输出中,将截断 ID 列以使其适合列表,并叠加列标题。自动调整列大小不会始终执行所需的操作。

    让列中的 Format-Table 输出自动换行 (Wrap)Wrapping Format-Table Output in Columns (Wrap)

    你可以通过使用 Wrap 参数让较长的 Format-Table 数据在其显示列中自动换行。仅使用 Wrap 参数不一定会实现所需的操作,因为如果你不同时指定 AutoSize,它会使用默认设置:

    1. PS> Get-Process -Name powershell | Format-Table -Wrap -Property Name,Id,Company,
    2. Path
    3. Name Id Company Path
    4. ---- -- ------- ----
    5. powershell 2836 Microsoft Corporati C:\Program Files\Wi
    6. on ndows PowerShell\v1
    7. .0\powershell.exe

    使用 Wrap 参数的一个优点是基本不会减慢进程速度。如果你对大型目录系统执行递归文件列表,那么如果你使用 AutoSize,可能得耗用大量时间和内存,才能显示第一批输出项。

    如果你并不关心系统负载,那么结合使用 AutoSizeWrap 参数则会获得良好的效果。会始终向初始列分配其所需的宽度以使其中的项显示在一行上,就和你指定 AutoSize 而不使用 Wrap 参数的效果一样。唯一的不同是最后一列将自动换行(如有必要的话):

    1. PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Name,I
    2. d,Company,Path
    3. Name Id Company Path
    4. ---- -- ------- ----
    5. powershell 2836 Microsoft Corporation C:\Program Files\Windows PowerShell\v1.0\
    6. powershell.exe

    组织选项卡输出 (-GroupBy)Organizing Table Output (-GroupBy)

    用于表格输出控制的另一个有用参数是 GroupBy较长的列表可能尤其难以进行比较。GroupBy 参数基于属性值对输出进行分组例如,我们可以按公司对进程分组以便于检查,同时从属性列表中省略公司的值:

    1. PS> Get-Process -Name powershell | Format-Table -Wrap -AutoSize -Property Name,I
    2. d,Path -GroupBy Company
    3. Company: Microsoft Corporation
    4. Name Id Path
    5. ---- -- ----
    6. powershell 1956 C:\Program Files\Windows PowerShell\v1.0\powershell.exe