Export (0) Print
Expand All

Formatting Command Output

Published: May 20, 2009

Updated: August 9, 2012

Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0

In traditional shells, each tool or command determines the format of its output. Some tools let you customize the output, and they include special parameters to control the output format.

In Windows PowerShell, the only cmdlets that format output are the format cmdlets:

  • Format-List

  • Format-Custom

  • Format-Table

  • Format-Wide

None of the other cmdlets format output. As a result, you do not need to learn the formatting routines and parameters of multiple tools. Just learn about the Format cmdlets and their parameters.

When you run a command, the Windows PowerShell calls the default formatter, which is determined by the type of data being displayed. The formatter determines which properties of the output are displayed and whether they are displayed in a list or table.

For example, when you use the Get-Service cmdlet, the default display is a three-column table, such as the following:

C:\PS> get-service
Status   Name               DisplayName
------   ----               -----------
Running  AdtAgent           Event Forwarder
Stopped  Alerter            Alerter
Running  ALG                Application Layer Gateway Service

To change the format of the output from any cmdlet, use the pipeline operator (|) to send the output of the command to a Format cmdlet.

For example, the following command sends the output of a Get-Service command to the Format-List cmdlet. As a result, the service data is formatted as a list for each service.

C:\PS> get-service | format-list
Name                : AdtAgent
DisplayName         : Event Forwarder
Status              : Running
DependentServices   : {}
ServicesDependedOn  : {eventlog, dnscache}
CanPauseAndContinue : False
CanShutdown         : True
CanStop             : True
ServiceType         : Win32OwnProcess

Name                : Alerter
DisplayName         : Alerter
Status              : Stopped
DependentServices   : {}
ServicesDependedOn  : {LanmanWorkstation}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : False
ServiceType         : Win32ShareProcess

Name                : ALG
DisplayName         : Application Layer Gateway Service
Status              : Running
DependentServices   : {}

In this format, not only does the data appear in a list, instead of a table, but there is more information about each service. Instead of three columns of data for each service, there are nine rows of data. Format-List did not retrieve the extra service information. The data was always there in the objects that Get-Service retrieved, but Format-Table, the default formatter, omitted it, because it could not display more than three columns across on the screen.

In addition to determining whether the data appears in a list or table, you can also determine which properties of the object are displayed. For example, the default display of Get-Service display only the Status, Name, and DisplayName properties of the service object.

To see all of the properties of an object, use a pipeline operator (|) to send the output of a command to the Get-Member cmdlet. For example, to see all of the properties of a service object, type:

get-service | get-member -membertype *property

   TypeName: System.ServiceProcess.ServiceController

Name                MemberType    Definition
----                ----------    ----------
Name                AliasProperty Name = ServiceName
CanPauseAndContinue Property      System.Boolean CanPauseAndContinue {get;}
CanShutdown         Property      System.Boolean CanShutdown {get;}
CanStop             Property      System.Boolean CanStop {get;}
Container           Property      System.ComponentModel.IContainer Container {get;}
DependentServices   Property      System.ServiceProcess.ServiceController[] DependentServices {get;}
DisplayName         Property      System.String DisplayName {get;set;}
MachineName         Property      System.String MachineName {get;set;}
ServiceHandle       Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}
ServiceName         Property      System.String ServiceName {get;set;}
ServicesDependedOn  Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}
ServiceType         Property      System.ServiceProcess.ServiceType ServiceType {get;}
Site                Property      System.ComponentModel.ISite Site {get;set;}
Status              Property      System.ServiceProcess.ServiceControllerStatus Status {get;}

Because all of these properties are in the object that Get-Service retrieves for each service, you can display any or all of them. Use the Property parameter of the Format cmdlets to select the properties to be displayed and the order in which they are displayed. For example, the following command uses the Format-Table command to display only the Name, ServiceType, and CanShutDown properties of the service.

get-service | format-table name, Servicetype, Canshutdown

This is just the beginning of what you can do with Windows PowerShell displays. For more details, use the following commands to read the help for the Format cmdlets:

get-help format-list
get-help format-table
get-help format-wide
get-help format-custom



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft