解释 PowerShell 的输出格式
powershellmicrosoft technologiessoftware & coding更新于 2026/1/11 14:22:17
在 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(Alias − 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 *) 或 (alias − 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 :

