How to Create Tables with Multiple Columns in PowerShell?

Recently, I got a requirement to create a table with multiple columns in PowerShell. This is a very common requirement among PowerShell developers. In this tutorial, I will explain how to create tables with multiple columns in PowerShell with examples.

To create a table with multiple columns in PowerShell, you can use custom objects and the Format-Table cmdlet. Start by initializing an array to store your custom objects. Add each row as a [PSCustomObject] with properties representing columns. Finally, pipe the array to Format-Table -AutoSize to display the table. For example:

$employees = @()
$employees += [PSCustomObject]@{ Name="John Smith"; Age=30; Role="Developer"; Salary=80000 }
$employees += [PSCustomObject]@{ Name="Jane Doe"; Age=25; Role="Designer"; Salary=70000 }
$employees | Format-Table -AutoSize

Create Tables with Multiple Columns in PowerShell

Tables are a powerful way to organize and display data in a readable format in PowerShell.

Now, let me show you how to create a table with multiple columns in PowerShell with examples.

Basic Syntax to Create Tables in PowerShell

To create a table in PowerShell, you primarily use custom objects and the Format-Table cmdlet. Here’s the basic syntax:

$Table = @()
$Table += [PSCustomObject]@{
    Column1 = "Value1"
    Column2 = "Value2"
}
$Table += [PSCustomObject]@{
    Column1 = "Value3"
    Column2 = "Value4"
}
$Table | Format-Table -AutoSize

Step-by-Step Guide

Here is the steps to create a table with multiple columns in PowerShell.

Step 1: Create an Array

First, create an array to store your custom objects:

$employees = @()

Step 2: Add Custom Objects

Next, add custom objects to the array. Each object represents a row in your table, and each property represents a column.

$employees += [PSCustomObject]@{
    Name   = "John Smith"
    Age    = 30
    Role   = "Developer"
    Salary = 80000
}

$employees += [PSCustomObject]@{
    Name   = "Jane Doe"
    Age    = 25
    Role   = "Designer"
    Salary = 70000
}

Step 3: Format the Table

Finally, pipe the array to the Format-Table cmdlet to display it as a table:

$employees | Format-Table -AutoSize

Here is the complete PowerShell script:

$employees = @()
$employees += [PSCustomObject]@{
    Name   = "John Smith"
    Age    = 30
    Role   = "Developer"
    Salary = 80000
}

$employees += [PSCustomObject]@{
    Name   = "Jane Doe"
    Age    = 25
    Role   = "Designer"
    Salary = 70000
}
$employees | Format-Table -AutoSize

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

Create Tables with Multiple Columns in PowerShell

Check out How to Use Sort With Format-Table in PowerShell?

Example 1: Create a Table with Multiple Columns

Let’s create a more detailed example where we generate a table of employees with columns for Name, Age, Role, and Salary.

$employees = @()

$employees += [PSCustomObject]@{
    Name   = "Alice Johnson"
    Age    = 28
    Role   = "Project Manager"
    Salary = 90000
}

$employees += [PSCustomObject]@{
    Name   = "Bob Brown"
    Age    = 35
    Role   = "System Administrator"
    Salary = 85000
}

$employees += [PSCustomObject]@{
    Name   = "Charlie Davis"
    Age    = 40
    Role   = "Database Administrator"
    Salary = 95000
}

$employees | Format-Table -AutoSize

Here is the exact output in the screenshot below:

powershell create table with multiple columns

Read PowerShell Format-Table Column Width

Example 2: Create a Table with Multiple Columns with Conditional Formatting

You can add conditional formatting to highlight specific rows or columns. Here’s an example that highlights employees earning more than $85,000:

$employees = @()

$employees += [PSCustomObject]@{
    Name   = "Alice Johnson"
    Age    = 28
    Role   = "Project Manager"
    Salary = 90000
}

$employees += [PSCustomObject]@{
    Name   = "Bob Brown"
    Age    = 35
    Role   = "System Administrator"
    Salary = 85000
}

$employees += [PSCustomObject]@{
    Name   = "Charlie Davis"
    Age    = 40
    Role   = "Database Administrator"
    Salary = 95000
}

$employees | ForEach-Object {
    if ($_.Salary -gt 85000) {
        $_ | Add-Member -MemberType NoteProperty -Name "HighEarner" -Value "Yes"
    } else {
        $_ | Add-Member -MemberType NoteProperty -Name "HighEarner" -Value "No"
    }
    $_
} | Format-Table -AutoSize

Here is the exact output in the screenshot below:

Create a Table with Multiple Columns in PowerShell

Check out PowerShell Format-Table

Example 3: Export the Table to a CSV File

You might want to export your table to a CSV file for further analysis. This can be done using the Export-Csv cmdlet. Here is the complete script:

$employees = @()

$employees += [PSCustomObject]@{
    Name   = "Alice Johnson"
    Age    = 28
    Role   = "Project Manager"
    Salary = 90000
}

$employees += [PSCustomObject]@{
    Name   = "Bob Brown"
    Age    = 35
    Role   = "System Administrator"
    Salary = 85000
}

$employees += [PSCustomObject]@{
    Name   = "Charlie Davis"
    Age    = 40
    Role   = "Database Administrator"
    Salary = 95000
}

$employees | Export-Csv -Path "C:\MyFolder\Employees.csv" -NoTypeInformation

Conclusion

In this tutorial, I explained how to create a table with multiple columns in PowerShell using Format-Table cmdlet with examples. Also, you got to know how to create tables with multiple columns in PowerShell with conditional formatting.

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.