Formatting
Powershell commands present their output in tables or lists, depending on the command being used and the attributes it is asked to display. The Select-Object command (abbreviated as Select) can be used to determine the attributes that are displayed. However, the display is limited to the attributes related to the invoked command. For example:
Get Process | select name,vm
...displays a list of processes, made up only of the name of each process and the amount of virtual memory it uses.
The formatting commands allow, in addition to printing the standard attributes, to display attributes calculated from the basic attributes. The display can be done in table, list or wide format mode.
Table Format
The Format-Table cmdlet (abbreviated as ft) allows you to display the output of a command in table format. The parameters received by this command are the following:
Parameters | Meaning |
---|---|
-Property | Allows you to specify the attributes (native or calculated) that you want to display. The wildcard * can be used to specify all of them. |
-Autosize | Allows the command to accommodate the width of the columns in the best possible way. Normally this width is fixed, but this parameter adjusts the width of the columns to the longest value of each attribute. |
-Groupby | This parameter specifies one of the fields. Each time there is a change in the value of this field, a new set of headers is printed. It is recommended to use Sort-Object before doing a Format-Table with Groupby, to avoid unnecessary repetition of headers. |
-Wrap | When a field's value is excessively long, Powershell wraps it, indicating this with an ellipsis (...). The Wrap parameter causes long values to span one or more additional lines, depending on the length of the value. |
Examples:
get-process | ft-Property *
get-process | ft -Property ID,Name,Responding -AutoSize
...gets a list of processes with ID, Name, and Responding attributes. The width of the columns is optimized.
get-process | ft -Property * -autosize
Get-Service | sortStatus | ft Name,Status,DisplayName -groupby Status
Get-Service | ft Name,Status,DisplayName -autosize -wrap
List Format
The Format-List cmdlet (abbreviated fl) allows you to display attributes as a series of name-value pairs, for example:
Get-Service -name bits | fl -Property *
Name : bits
RequiredServices : {RpcSs}
CanPauseAndContinue : False
CanShutdown : False
CanStop : False
DisplayName : Background Intelligent Transfer Service
DependentServices : {}
MachineName : .
ServiceName : bits
ServicesDependedOn : {RpcSs}
ServiceHandle :
Status : Stopped
ServiceType : Win32ShareProcess
StartType : Manual
Site :
Container :
Note that the output of the Get-Service command was filtered here, to get only the information for the BITS service (otherwise, Format-List would have returned a similar set of lines for each service). Here Format-List was asked to display all attributes of the service (the -Property parameter with wildcard).
Wide Format
The wide format allows you to display two or more columns of a particular object property, much like the Linux ls command. The Format-Wide (abbreviated fw) cmdlet is used for this purpose.
Examples:
get-process | format-wide
get-process | format-wide name -col 4
Change Of Names Of Attributes
An example of renaming an attribute is the following:
get service | ft @{name='Service';expression={$_.Name}},Status,DisplayName
In this case, a new column is defined, using the expression between the @{ } symbols.
- Name indicates the name that the column will have. It can be abbreviated as n.
- Expression defines the content of the column. It can be abbreviated as e.
- $ is a special variable that holds the object that is currently being processed. For this example, $.Name means: "Of the current object, take the Name property."
The above command could be abbreviated like this:
get service | ft @{n='Service';e={$_.Name}},Status,DisplayName
get-process | ft Name,@{n='VM (MB)';e={$_.VM / 1MB}}
The command:
get-process | ft Name,@{n='VM (MB)';e={$_.VM / 1MB -as [int]}} -AutoSize
Using The Format Commands
The formatting command to be used (ft, fl or fw) must be the last one in the pipeline before printing to the screen. The output of a format command can only be redirected to a TEXT file. If you try to convert the output to another format (CSV, HTML, XML) the results will be inconsistent.