Explain output formatting PowerShell


In PowerShell, the output is in the default table format. To format the output in the desired format, mainly below pipeline commands are used.

Format-Table

Format-List

Format-Wide

There are also commands to format the output data but not used widely.

Format-Custom

Format-Hex

In the below example, we will get the service's details using Get-Service and format it using the different output cmdlets.

Get-Service WinRM, Spooler

Output

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

As you can see Display Name for the WinRM service is not displayed fully because of the display of output character restriction in the default format. To get the full output you need to use the Format-Table(Alias − FT) and its –AutoSize Parameter.

Example

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

Output

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

You can also wrap the output of the command using –Wrap in Format-Table command.

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

If you need output without any header, use –HideTableHeaders parameter in Format-Table command. You can combine two parameters as shown below.

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

Output

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

In the above output, you don’t get all the properties of the services with those pipeline commands. To get all the properties of the services you need to use (Format-List *) or (alias − fl *) command.

Example

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           :

Updated on: 26-Mar-2020

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements