Format Table - PowerShell
Overview
Format-Table
formats a list of objects or custom objects as a table. It presents the data in a tabular format, with column headers representing the properties of the objects. This command is useful for organizing and displaying data in a readable and structured way.
Syntax
Format-Table [-AutoSize] [-Wrap] [-Column <String>] [-First <Int32>] [-Header <String>] [-HideTableHeaders] [-Last <Int32>] [-OutFile <String>] [-OutVariable <String>] [-GroupBy <String>] [-Width <Int32>] [-AutoSize] [-Wrap] [-Property <String>] [-Where <String>] [-Select <String>]
Options/Flags
- -AutoSize: Automatically adjusts the width of the columns to fit the data.
- -Wrap: Wraps the text within the columns to prevent truncation.
- -Column: Specifies the properties to be displayed as columns.
- -First: Limits the number of rows displayed to the specified number from the beginning.
- -Header: Sets the header text for the table.
- -HideTableHeaders: Hides the table headers.
- -Last: Limits the number of rows displayed to the specified number from the end.
- -OutFile: Saves the formatted table as a CSV file in the specified location.
- -OutVariable: Stores the formatted table in the specified variable.
- -GroupBy: Groups the objects by the specified property and displays the results as a nested table.
- -Width: Sets the width of the table in characters.
- -Property: Same as
-Column
. - -Where: Filters the objects based on the specified condition.
- -Select: Selects only the specified properties to be displayed.
Examples
Simple Formatting:
PS> Get-Process | Format-Table -AutoSize
Custom Headers and Filtering:
PS> Get-Service | Format-Table -Header ServiceName, Status -Where {$_.Status -like "Running"}
Grouping and Wrapping:
PS> Get-EventLog -LogName System | Format-Table -GroupBy Source -AutoSize -Wrap
Common Issues
- Truncated Text: Enable text wrapping (
-Wrap
) to display long text in full. - Unformatted Output: Ensure that the objects to be formatted have valid properties.
Integration
Format-Table
can be combined with other commands for advanced tasks:
Piping to CSV:
PS> Get-ChildItem | Format-Table -AutoSize | Out-File file.csv
Conditional Formatting:
PS> Get-EventLog -LogName System | Format-Table -GroupBy Source -AutoSize | Where-Object {$_.Source -eq "Service Control Manager"}
Related Commands
Get-Member
: Gets the members (properties and methods) of objects.Export-Csv
: Exports a list of objects as a CSV file.Measure-Object
: Measures properties of objects and displays the results in a table.