PowerShell Format-Table Column Width

As a PowerShell developer, you might have used the Format-Table cmdlet to display data in a readable and organized manner. In this tutorial, I will explain various methods to control column widths using Format-Table in PowerShell with examples.

PowerShell Format-Table Column Width

Format-Table is a PowerShell cmdlet that formats the output as a table with the selected properties of the object. This cmdlet is highly useful when you need to present data in a structured format. By default, PowerShell decides the width of the columns based on the data. However, you can customize the column widths to suit your needs.

The basic syntax of the Format-Table cmdlet is as follows:

Format-Table [-Property] <Object[]> [-AutoSize] [-Wrap] [-HideTableHeaders] [-GroupBy <Object>] [-View <String>] [<CommonParameters>]

There are several ways to control the column widths in Format-Table. Let me show you these methods with examples.

1. Using the -AutoSize Parameter

The -AutoSize parameter adjusts the column widths to minimize the amount of unused space in the table. This is useful when you want to ensure that the table fits nicely within the console window.

Get-Process | Format-Table -Property Name, CPU, ID -AutoSize

In this example, the -AutoSize parameter ensures that the columns for Name, CPU, and ID are adjusted to fit the data neatly.

2. Specify Column Widths Manually

You can manually specify the width of each column using a calculated property. This method gives you full control over the column widths.

Get-Process | Format-Table @{Label="Name";Expression={$_.Name};Width=20}, @{Label="CPU";Expression={$_.CPU};Width=10}, @{Label="ID";Expression={$_.ID};Width=10}

In this example, the Name column is set to a width of 20, while the CPU and ID columns are set to a width of 10.

Here is the output you can see in the screenshot below:

powershell format-table column width

3. Using the -Wrap Parameter

The -Wrap parameter allows the text to wrap within the column if it exceeds the column width. This is particularly useful for columns with long text values.

Get-Service | Format-Table -Property Name, DisplayName -Wrap -AutoSize

In this example, the -Wrap parameter ensures that the DisplayName column wraps the text if it is too long, while -AutoSize adjusts the column widths.

Check out Create an HTML Table from Variables in PowerShell

PowerShell Format-Table Column Width Examples

Now, let me show you two real examples of setting width in the PowerShell Format-Table Column.

Example 1: Display Employee Data

Here is how to display Employee data using PowerShell Format-Table.

$employees = @(
    [PSCustomObject]@{Name="John Smith"; Position="Software Engineer"; Department="IT"},
    [PSCustomObject]@{Name="Jane Doe"; Position="Project Manager"; Department="Operations"},
    [PSCustomObject]@{Name="Michael Johnson"; Position="Data Analyst"; Department="Finance"}
)

$employees | Format-Table @{Label="Employee Name";Expression={$_.Name};Width=20}, @{Label="Position";Expression={$_.Position};Width=25}, @{Label="Department";Expression={$_.Department};Width=15} -AutoSize

In this example, we format a table of employee data, specifying the widths for Employee Name, Position, and Department columns.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

powershell format-table fixed column width

Example 2: Display City Population Data

Here is another example of displaying city population data and setting column width.

$cities = @(
    [PSCustomObject]@{City="New York"; Population=8419000; State="NY"},
    [PSCustomObject]@{City="Los Angeles"; Population=3980000; State="CA"},
    [PSCustomObject]@{City="Chicago"; Population=2716000; State="IL"}
)

$cities | Format-Table @{Label="City";Expression={$_.City};Width=15}, @{Label="Population";Expression={$_.Population};Width=10}, @{Label="State";Expression={$_.State};Width=5} -AutoSize

In this example, we format a table of city population data, specifying the widths for City, Population, and State columns.

Check out Convert String to HTML Table in PowerShell

PowerShell Format-Table Fixed Column Width

When working with PowerShell, Format-Table is an invaluable cmdlet for presenting data in a tabular format. However, sometimes, the default column widths may not suit your needs, especially when you want to ensure that columns have a fixed width for better readability and consistency.

To set fixed column widths, you can use calculated properties within Format-Table. This involves creating a custom label and expression for each column and specifying the desired width.

Let me show you an example.

Suppose you have a list of students and their grades. You want to display this information in a table with fixed column widths for better alignment and readability. Here’s how you can achieve that:

# Create a list of students with their grades
$students = @(
    [PSCustomObject]@{Name="Alice Johnson"; Subject="Mathematics"; Grade="A"},
    [PSCustomObject]@{Name="Bob Smith"; Subject="History"; Grade="B"},
    [PSCustomObject]@{Name="Charlie Brown"; Subject="Science"; Grade="A"},
    [PSCustomObject]@{Name="Diana Ross"; Subject="English"; Grade="C"}
)

# Format the table with fixed column widths
$students | Format-Table @{Label="Student Name";Expression={$_.Name};Width=20}, 
                            @{Label="Subject";Expression={$_.Subject};Width=15}, 
                            @{Label="Grade";Expression={$_.Grade};Width=5}

Format-Table with Fixed Widths: We then pipe this list to Format-Table and define the columns with fixed widths.

  • @{Label="Student Name";Expression={$_.Name};Width=20}: This sets the Student Name column to a fixed width of 20 characters.
  • @{Label="Subject";Expression={$_.Subject};Width=15}: This sets the Subject column to a fixed width of 15 characters.
  • @{Label="Grade";Expression={$_.Grade};Width=5}: This sets the Grade column to a fixed width of 5 characters.

The output will be a neatly aligned table with fixed column widths, ensuring that each column maintains its specified width regardless of the content length. Here’s what the output will look like:

Student Name         Subject        Grade
------------         -------        -----
Alice Johnson        Mathematics    A    
Bob Smith            History        B    
Charlie Brown        Science        A    
Diana Ross           English        C    

Here is the exact output in the screenshot below:

powershell format-table custom column width

Check out Format an Array as an HTML Table in PowerShell

PowerShell Format-Table Custom Column Width

While working with the Fortat-Table cmdlet, sometimes you need to set custom column widths using Format-Table in PowerShell.

To customize column widths in Format-Table, you can use calculated properties. This involves creating custom labels and expressions for each column and specifying the desired width.

Now, let me show you an example.

Imagine you have a list of employees with their names, job titles, and departments. You want to display this information in a table with custom column widths for better alignment and readability. Here’s how you can achieve that using the below PowerShell script.

# Create a list of employees with their job titles and departments
$employees = @(
    [PSCustomObject]@{Name="John Smith"; JobTitle="Software Engineer"; Department="IT"},
    [PSCustomObject]@{Name="Jane Doe"; JobTitle="Project Manager"; Department="Operations"},
    [PSCustomObject]@{Name="Michael Johnson"; JobTitle="Data Analyst"; Department="Finance"},
    [PSCustomObject]@{Name="Emily Davis"; JobTitle="HR Specialist"; Department="Human Resources"}
)

# Format the table with custom column widths
$employees | Format-Table @{Label="Employee Name";Expression={$_.Name};Width=20}, 
                            @{Label="Job Title";Expression={$_.JobTitle};Width=25}, 
                            @{Label="Department";Expression={$_.Department};Width=20}

Here

  • Creating the List: We start by creating a list of employees using PSCustomObject. Each employee has a NameJobTitle, and Department.
  • Format-Table with Custom Widths: We then pipe this list to Format-Table and define the columns with custom widths.
    • @{Label="Employee Name";Expression={$_.Name};Width=20}: This sets the Employee Name column to a width of 20 characters.
    • @{Label="Job Title";Expression={$_.JobTitle};Width=25}: This sets the Job Title column to a width of 25 characters.
    • @{Label="Department";Expression={$_.Department};Width=20}: This sets the Department column to a width of 20 characters.

The output will be a neatly aligned table with custom column widths, ensuring that each column maintains its specified width regardless of the content length. Here’s what the output will look like:

Employee Name        Job Title                 Department          
-------------        ---------                 ----------          
John Smith           Software Engineer         IT                  
Jane Doe             Project Manager           Operations          
Michael Johnson      Data Analyst              Finance             
Emily Davis          HR Specialist             Human Resources     

Here is the exact output in the screenshot below:

powershell format-table specify column width

Conclusion

I explained how to set width in this tutorial while working with the PowerShell Format-Table cmdlet. You can use parameters like -AutoSize, -Wrap, etc. You can also specify column widths manually. Do let me know in the comment below if all the above examples help you.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.