PowerShell 输出格式化说明


在 PowerShell 中,输出默认为表格格式。要将输出格式化为所需的格式,主要使用以下管道命令。

Format-Table

Format-List

Format-Wide

还有一些命令可以格式化输出数据,但使用并不广泛。

Format-Custom

Format-Hex

在下面的示例中,我们将使用 **Get-Service** 获取服务的详细信息,并使用不同的输出 cmdlet 对其进行格式化。

Get-Service WinRM, Spooler

输出

Status     Name        DisplayName
------     ----        -----------
Running    Spooler     Print Spooler
Stopped    WinRM       Windows Remote Management (WS-Manag...

正如您所看到的,由于默认格式中输出字符限制的缘故,WinRM 服务的显示名称并未完全显示。要获取完整的输出,您需要使用 **Format-Table**(别名 − FT) 及其 –**AutoSize** 参数。

示例

Get-Service WinRM, Spooler | Format-Table -AutoSize

输出

Status   Name      DisplayName
------   ----      -----------
Running  Spooler   Print Spooler
Stopped  WinRM     Windows Remote Management (WS-Management)

您也可以使用 **Format-Table** 命令中的 –**Wrap** 来换行输出。

Status      Name        DisplayName
------      ----        -----------
Running     Spooler     Print Spooler
Stopped     WinRM       Windows Remote Management (WS-Management)

如果您需要不带任何标题的输出,请在 **Format-Table** 命令中使用 –**HideTableHeaders** 参数。您可以组合使用两个参数,如下所示。

Get-Service WinRM,Spooler | Format-Table –HideTableHeaders -AutoSize

输出

Running Spooler Print Spooler
Stopped WinRM Windows Remote Management (WS-Management)

在上面的输出中,您无法通过这些管道命令获得服务的全部属性。要获取服务的全部属性,您需要使用 (**Format-List ***) 或 (**别名 − fl ***) 命令。

示例

PS C:\WINDOWS\system32> Get-Service Spooler | fl *
Name                : Spooler
RequiredServices    : {RPCSS, http}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : True
DisplayName         : Print Spooler
DependentServices   : {Fax}
MachineName         : .
ServiceName         : Spooler
ServicesDependedOn  : {RPCSS, http}
ServiceHandle       : SafeServiceHandle
Status              : Running
ServiceType         : Win32OwnProcess, InteractiveProcess
StartType           : Automatic
Site                :
Container           :

更新于:2020年3月26日

452 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.