How to Create a Table with Headers in PowerShell?

In today’s tutorial, I will explain how to create a table with headers in PowerShell. As a PowerShell expert, you should know this. Let me show you different methods to show this with examples.

To create a table with headers in PowerShell using the Format-Table cmdlet, you can pipe the output of a command into Format-Table and specify the properties you want as columns. For example, running Get-Process | Format-Table -Property Name, Id, CPU will display a table with headers “Name,” “Id,” and “CPU,” listing the corresponding details of running processes.

Create a Table with Headers in PowerShell

Tables allow you to format output in a structured way, making it easier to read and understand. This is particularly useful when dealing with large data sets or when you need to present information clearly and concisely.

PowerShell provides different methods for creating tables with headers. Let me show you each method with examples.

Method 1: Using Format-Table

The Format-Table cmdlet is mostly used to create a table with headers in PowerShell. It formats the output of a command as a table with the selected properties of the object in each column.

Syntax:

Here is the syntax:

Get-Process | Format-Table -Property Name, Id, CPU

Example:

Let me show you an example.

Let’s say you want to list the running processes on your computer with their names, IDs, and CPU usage:

Get-Process | Format-Table -Property Name, Id, CPU

This command will output a table with headers: Name, Id, and CPU.

Here is the exact output in the screenshot below:

powershell create table with headers

Check out PowerShell Format-Table Column Width

Method 2: Using Custom Objects

Creating custom objects in PowerShell allows for more flexibility and control over the data you display. This method is particularly useful when creating a table from scratch or combining data from different sources.

Syntax

Here is the syntax:

$Table = @()
$Table += [PSCustomObject]@{Name="John Doe"; Age=30; City="New York"}
$Table += [PSCustomObject]@{Name="Jane Smith"; Age=25; City="Los Angeles"}
$Table | Format-Table -AutoSize

Example

Now, let me show you an example.

Here’s how you can create a table with custom headers for a list of people:

$people = @()
$people += [PSCustomObject]@{Name="John Doe"; Age=30; City="New York"}
$people += [PSCustomObject]@{Name="Jane Smith"; Age=25; City="Los Angeles"}
$people += [PSCustomObject]@{Name="Michael Johnson"; Age=40; City="Chicago"}

$people | Format-Table -AutoSize

This will output a table with headers: Name, Age, and City, showing the data for each person.

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

Create a Table with Headers in PowerShell

Check out PowerShell Format-Table

Method 3: Using Out-GridView

Out-GridView is a cmdlet that sends output to an interactive table in a separate window. This is useful for larger datasets where scrolling and sorting might be necessary. This is another way to create a table with headers in PowerShell.

Syntax

Here is the syntax:

Get-Service | Out-GridView

Example

Here is an example.

To display a list of services on your computer in an interactive table:

Get-Service | Out-GridView

This command will open a new window with a table that includes headers for the service properties.

Check out Create an HTML Table from Variables in PowerShell

Method 4: Creating Tables in Word Documents

Sometimes, you might need to export your table data to a Word document. PowerShell can automate this process, making it easy to create formatted documents.

Syntax

Here is the syntax:

$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Add()
$table = $doc.Tables.Add($doc.Range(), 3, 3)
$table.Cell(1,1).Range.Text = "Name"
$table.Cell(1,2).Range.Text = "Age"
$table.Cell(1,3).Range.Text = "City"

Example

Let me show you an example.

Here’s the complete PowerShell script to create a table in a Word document with headers:

$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Add()
$table = $doc.Tables.Add($doc.Range(), 4, 3)

# Adding headers
$table.Cell(1,1).Range.Text = "Name"
$table.Cell(1,2).Range.Text = "Age"
$table.Cell(1,3).Range.Text = "City"

# Adding data
$table.Cell(2,1).Range.Text = "John Doe"
$table.Cell(2,2).Range.Text = "30"
$table.Cell(2,3).Range.Text = "New York"
$table.Cell(3,1).Range.Text = "Jane Smith"
$table.Cell(3,2).Range.Text = "25"
$table.Cell(3,3).Range.Text = "Los Angeles"
$table.Cell(4,1).Range.Text = "Michael Johnson"
$table.Cell(4,2).Range.Text = "40"
$table.Cell(4,3).Range.Text = "Chicago"

You can see the exact output like in the screenshot below;

create table with headers in powershell

Conclusion

In this tutorial, I explained different methods to create a table with headers in PowerShell, such as using Format-Table for quick formatting, custom objects for more control, Out-GridView for interactive tables, or exporting to Word documents, etc.

I tried all these methods and got the exact required output. Do let me know if you still have any questions.

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.