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 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告