How to Adjust the Width with Format-Table in PowerShell
In PowerShell, you can adjust and control the width when using the Format-Table cmdlet in several ways.
Method 1: Format-Table Column Width Using the AutoSize parameter
Get-Process | Format-Table -AutoSize
This example adjusts the column widths to fit the content by using the Autosize parameter.
Method 2: Format-Table Column Width Using the Width parameter
Another way to limit the width of a specific column is by creating a calculated property with a custom expression.
Get-Process | Format-Table `
@{Label="Name"; Expression={$_.Name}; Width=20}, `
@{Label="Id"; Expression={$_.Id}; Width=8}, `
@{Label="CPU"; Expression={$_.CPU}; Width=10}, `
@{Label="Memory"; Expression={$_.Memory}; Width=12} `
-AutoSize
This example adjusts the column width for each column individually in Format-Table.
Method 3: Format-Table Column Width with Out-String cmdlet
Get-Process | Format-Table | Out-String -Width 300
This example controls the width of the entire table by piping the output to the Out-String cmdlet with a specific width value.
All these methods can be used to control the width of columns in the Format-Table output to a table.
The following example shows how to use each of the methods.
Format-Table Column Width Using the AutoSize parameter
The Autosize parameter adjusts the width to fit the content. This ensures that the columns are wide enough to show all values without truncation.
The following PowerShell script shows how to do it with syntax.
Get-Process | Format-Table -AutoSize
The Get-Process cmdlet retrieves the list of all currently running processes and pipes them to the Format-Table to output to a table with column width adjusted.
Format-Table Width Using the Width parameter
You can limit the width of a specific column by creating a calculated property with a custom expression.
The following PowerShell script shows how to do it with syntax.
Get-Process | Format-Table `
@{Label="Name"; Expression={$_.Name}; Width=20}, `
@{Label="Id"; Expression={$_.Id}; Width=8}, `
@{Label="CPU"; Expression={$_.CPU}; Width=10}, `
@{Label="Memory"; Expression={$_.Memory}; Width=12} `
-AutoSize
In this example, We use the Format-Table cmdlet to format the output of the Get-Process cmdlet. For each column, we specify a calculated property using @{} syntax.
Inside each calculated property, we define the Label, Expression, and Width parameters to adjust the width for each column as needed to set custom widths.
Format-Table Width Using the Out-String cmdlet
Another way to control the width of the table is by piping the output of the Format-Table to the Out-String with a specific width value.
The following PowerShell script shows how to do it with syntax.
Get-Process | Format-Table | Out-String -Width 300
This example adjusts the width of the entire table. Note that, Out-String with Format-Table may result in less visually appealing output on the screen.
Conclusion
I hope the above article on how to control the width of a format table in PowerShell is helpful to you.
Related Articles
Output Formatting
- PowerShell Output Table - Output to tables
- PowerShell Select-Object - Select and format properties
- PowerShell Output to File - Output to files
- PowerShell Format List - Alternative formatting method
Data Processing & Selection
- PowerShell Where-Object - Filter data before formatting
- PowerShell ForEach-Object - Process rows
- PowerShell Sort-Object - Sort before formatting
- PowerShell Group-Object - Group data
- PowerShell Measure-Object - Aggregate data
Data Sources for Tables
- PowerShell Get-Process - Process data
- PowerShell Get-Service - Service data
- PowerShell Get-ChildItem - File listings
- PowerShell Get File Properties - File metadata
- PowerShell List Files - File tables
Custom Properties
- PowerShell Add-Member - Add custom properties
- PowerShell Calculated Properties - Create custom columns
- PowerShell Strings - String formatting in columns
Data Types & Conversion
- PowerShell DateTime Format - Format dates in tables
- PowerShell Convert - Convert data types for display
- PowerShell Data Types - Type handling
Control Flow & Logic
- PowerShell If-Else Statement - Conditional formatting
- PowerShell Switch Statement - Switch-based logic
- PowerShell Try-Catch - Error handling
Data Storage
- PowerShell Arrays - Work with data arrays
- PowerShell Hashtables - Work with objects
- PowerShell Variables - Store data
Export & Files
- PowerShell Export CSV - Export to CSV
- PowerShell Import CSV - Import from CSV
- PowerShell Get-Content - Read file data
Functions & Organization
- PowerShell Functions - Create formatting functions
- PowerShell Complete Guide - Full PowerShell guide
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.