Using Format commands to change output view - PowerShell (2024)

  • Article

PowerShell has a set of cmdlets that allow you to control how properties are displayed forparticular objects. The names of all the cmdlets begin with the verb Format. They let you selectwhich properties you want to show.

Get-Command -Verb Format -Module Microsoft.PowerShell.Utility
CommandType Name Version Source----------- ---- ------- ------Cmdlet Format-Custom 6.1.0.0 Microsoft.PowerShell.UtilityCmdlet Format-Hex 6.1.0.0 Microsoft.PowerShell.UtilityCmdlet Format-List 6.1.0.0 Microsoft.PowerShell.UtilityCmdlet Format-Table 6.1.0.0 Microsoft.PowerShell.UtilityCmdlet Format-Wide 6.1.0.0 Microsoft.PowerShell.Utility

This article describes the Format-Wide, Format-List, and Format-Table cmdlets.

Each object type in PowerShell has default properties that are used when you don't select theproperties to display. Each cmdlet uses the same Property parameter to specify which propertiesyou want displayed. Because Format-Wide only shows a single property, its Property parameteronly takes a single value, but the property parameters of Format-List and Format-Table accept alist of property names.

In this example, the default output of Get-Process cmdlet shows that we've two instances ofInternet Explorer running.

Get-Process -Name iexplore

The default format for Process objects displays the properties shown here:

 NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 32 25.52 10.25 13.11 12808 1 iexplore 52 11.46 26.46 3.55 21748 1 iexplore

Using Format-Wide for single-item output

The Format-Wide cmdlet, by default, displays only the default property of an object. Theinformation associated with each object is displayed in a single column:

Get-Command -Verb Format | Format-Wide
Format-Custom Format-HexFormat-List Format-TableFormat-Wide

You can also specify a non-default property:

Get-Command -Verb Format | Format-Wide -Property Noun
Custom HexList TableWide

Controlling Format-Wide display with column

With the Format-Wide cmdlet, you can only display a single property at a time. This makes ituseful for displaying large lists in multiple columns.

Get-Command -Verb Format | Format-Wide -Property Noun -Column 3
Custom Hex ListTable Wide

Using Format-List for a list view

The Format-List cmdlet displays an object in the form of a listing, with each property labeled anddisplayed on a separate line:

Get-Process -Name iexplore | Format-List
Id : 12808Handles : 578CPU : 13.140625SI : 1Name : iexploreId : 21748Handles : 641CPU : 3.59375SI : 1Name : iexplore

You can specify as many properties as you want:

Get-Process -Name iexplore | Format-List -Property ProcessName,FileVersion,StartTime,Id
ProcessName : iexploreFileVersion : 11.00.18362.1 (WinBuild.160101.0800)StartTime : 10/22/2019 11:23:58 AMId : 12808ProcessName : iexploreFileVersion : 11.00.18362.1 (WinBuild.160101.0800)StartTime : 10/22/2019 11:23:57 AMId : 21748

Getting detailed information using Format-List with wildcards

The Format-List cmdlet lets you use a wildcard as the value of its Property parameter. Thislets you display detailed information. Often, objects include more information than you need, whichis why PowerShell doesn't show all property values by default. To show all properties of an object,use the Format-List -Property * command. The following command generates more than 60 lines ofoutput for a single process:

Get-Process -Name iexplore | Format-List -Property *

Although the Format-List command is useful for showing detail, if you want an overview of outputthat includes many items, a simpler tabular view is often more useful.

Using Format-Table for tabular output

If you use the Format-Table cmdlet with no property names specified to format the output of theGet-Process command, you get exactly the same output as you do without a Format cmdlet. Bydefault, PowerShell displays Process objects in a tabular format.

Get-Service -Name win* | Format-Table
Status Name DisplayName------ ---- -----------Running WinDefend Windows Defender Antivirus ServiceRunning WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...Running Winmgmt Windows Management InstrumentationRunning WinRM Windows Remote Management (WS-Manag...

Note

Get-Service is only available on Windows platforms.

Improving Format-Table output

Although a tabular view is useful for displaying lots of information, it may be difficult tointerpret if the display is too narrow for the data. In the previous example, the output istruncated. If you specify the AutoSize parameter when you run the Format-Table command,PowerShell calculates column widths based on the actual data displayed. This makes the columnsreadable.

Get-Service -Name win* | Format-Table -AutoSize
Status Name DisplayName------ ---- -----------Running WinDefend Windows Defender Antivirus ServiceRunning WinHttpAutoProxySvc WinHTTP Web Proxy Auto-Discovery ServiceRunning Winmgmt Windows Management InstrumentationRunning WinRM Windows Remote Management (WS-Management)

The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.Properties, other than the last one displayed, are given as much size as they need for their longestdata element to display correctly.

Get-Service -Name win* | Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -AutoSize
Name Status StartType DisplayName DependentServi ces---- ------ --------- ----------- --------------WinDefend Running Automatic Windows Defender Antivirus Service {}WinHttpAutoProxySvc Running Manual WinHTTP Web Proxy Auto-Discovery Service {NcaSvc, iphl…Winmgmt Running Automatic Windows Management Instrumentation {vmms, TPHKLO…WinRM Running Automatic Windows Remote Management (WS-Management) {}

The Format-Table command assumes that properties are listed in order of importance. The cmdletattempts to fully display the properties nearest the beginning. If the Format-Table command can'tdisplay all the properties, it removes some columns from the display. You can see this behavior inthe DependentServices property previous example.

Wrapping Format-Table output in columns

You can force lengthy Format-Table data to wrap within its display column using the Wrapparameter. Using the Wrap parameter may not do what you expect, since it uses default settingsif you don't also specify AutoSize:

Get-Service -Name win* | Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -Wrap
Name Status StartType DisplayName DependentServi ces---- ------ --------- ----------- --------------WinDefend Running Automatic Windows Defender Antivirus Service {}WinHttpAutoProxySvc Running Manual WinHTTP Web Proxy Auto-Discovery Service {NcaSvc, iphlpsvc}Winmgmt Running Automatic Windows Management Instrumentation {vmms, TPHKLOAD, SUService, smstsmgr…}WinRM Running Automatic Windows Remote Management (WS-Management) {}

Using the Wrap parameter by itself doesn't slow down processing very much. However, usingAutoSize to format a recursive file listing of a large directory structure can take a long timeand use lots of memory before displaying the first output items.

If you aren't concerned about system load, then AutoSize works well with the Wrap parameter.The initial columns still use as much width as needed to display items on one line, but the finalcolumn is wrapped, if necessary.

Note

Some columns may not be displayed when you specify the widest columns first. For best results,specify the smallest data elements first.

In the following example, we specify the widest properties first.

Get-Process -Name iexplore | Format-Table -Wrap -AutoSize -Property FileVersion, Path, Name, Id

Even with wrapping, the final Id column is omitted:

FileVersion Path Nam e----------- ---- ---11.00.18362.1 (WinBuild.160101.0800) C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE iex plo re11.00.18362.1 (WinBuild.160101.0800) C:\Program Files\Internet Explorer\iexplore.exe iex plo re

Organizing table output

Another useful parameter for tabular output control is GroupBy. Longer tabular listings inparticular may be hard to compare. The GroupBy parameter groups output based on a propertyvalue. For example, we can group services by StartType for easier inspection, omitting theStartType value from the property listing:

Get-Service -Name win* | Sort-Object StartType | Format-Table -GroupBy StartType
 StartType: AutomaticStatus Name DisplayName------ ---- -----------Running WinDefend Windows Defender Antivirus ServiceRunning Winmgmt Windows Management InstrumentationRunning WinRM Windows Remote Management (WS-Managem… StartType: ManualStatus Name DisplayName------ ---- -----------Running WinHttpAutoProxyS… WinHTTP Web Proxy Auto-Discovery Serv…
Using Format commands to change output view - PowerShell (2024)
Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 5517

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.