PowerShell Write-Host Array as Table | PowerShell format array as Table

Recently, I got a requirement to format an array as table in PowerShell. I used Write-Host as well as Format-Table. In this PowerShell tutorial, I will explain how to display the elements of an array as a table using the Write-Host cmdlet in PowerShell. And, also how to use Format-Table to format an array as Table in PowerShell.

In PowerShell, you can display an array as a table using the Write-Host cmdlet combined with a loop or other formatting commands. However, Write-Host by itself does not format output as a table. Instead, you would typically use Format-Table to format the array, and then output each line with Write-Host. For example: $array | Format-Table | Out-String -Stream | ForEach-Object { Write-Host $_ }. This command sequence formats the array as a table, converts it to a string, and then outputs each line individually with Write-Host.

PowerShell Write-Host Array as Table

Suppose, I have a PowerShell array having the below elements.

$myArray = "Item1", "Item2", "Item3"

Now, we will see how to use the Write-Host cmdlet.

The Write-Host cmdlet in PowerShell is used to display output to the console. However, it is not the best tool for creating tables because it doesn’t have built-in features for formatting arrays as tables. When you pass an array to Write-Host, it will simply display the array elements separated by spaces:

Write-Host $myArray

Output:

Item1 Item2 Item3

Format array as Table in PowerShell

While Write-Host is limited, PowerShell has other cmdlets that are more suited for displaying arrays as tables, such as Format-Table or creating custom tables using calculated properties.

Using Format-Table

In PowerShell, you can format an array as a table using the Format-Table cmdlet, which allows you to display the elements of an array in a structured, tabular format.

Here is an example.

$myArray = "Item1", "Item2", "Item3"
$myArray | Format-Table

However, Format-Table is primarily used with arrays of objects where each object represents a row, and the object properties represent columns.

Here’s a basic example of how to use Format-Table with an array of objects:

# Define an array of custom objects
$people = @(
    @{Name='Alice'; Age=25; Job='Engineer'},
    @{Name='Bob'; Age=30; Job='Designer'},
    @{Name='Charlie'; Age=28; Job='Architect'}
)

# Convert the array to objects and format as a table
$people | ForEach-Object { New-Object PSObject -Property $_ } | Format-Table -AutoSize

In this example, we create an array of hash tables, each representing a person with NameAge, and Job properties. We then pipe (|) the array to ForEach-Object, where we convert each hash table to a PSObject. Finally, we pipe the objects to Format-Table with the -AutoSize parameter, which adjusts the column widths to fit the content.

The output will appear as a table:

Name     Age Job      
----     --- ---      
Alice    25  Engineer 
Bob      30  Designer 
Charlie  28  Architect

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

PowerShell format array as Table

For more advanced formatting, you can customize which properties to display and in what order by specifying them in the Format-Table cmdlet. For instance, if you only want to display the Name and Job properties:

$people | ForEach-Object { New-Object PSObject -Property $_ } | Format-Table Name, Job -AutoSize

This will output:

Name     Job      
----     ---      
Alice    Engineer 
Bob      Designer 
Charlie  Architect

Create a Custom Table to Format Array As Table in PowerShell

If you want to display a simple array as a table with headers in PowerShell, you might need to create custom objects and then pipe them to Format-Table. Here’s an example:

$myArray = @(
    [PSCustomObject]@{Element="Item1"},
    [PSCustomObject]@{Element="Item2"},
    [PSCustomObject]@{Element="Item3"}
)

$myArray | Format-Table -Property Element

Output:

Element
-------
Item1
Item2
Item3

You can see the output in the screenshot below:

PowerShell Write-Host Array as Table

Multi-Column Table from a Multi-Dimensional Array in PowerShell

For a more advanced scenario, let’s say you have a multi-dimensional array and want to display it in a table with multiple columns. You can create a custom table like this:

$multiArray = @(
    @("Item1", "Description1", "Value1"),
    @("Item2", "Description2", "Value2"),
    @("Item3", "Description3", "Value3")
)

$customTable = $multiArray | ForEach-Object {
    [PSCustomObject]@{
        Item = $_[0]
        Description = $_[1]
        Value = $_[2]
    }
}

$customTable | Format-Table

Output:

Item   Description   Value
----   -----------   -----
Item1  Description1  Value1
Item2  Description2  Value2
Item3  Description3  Value3

Conclusion

While Write-Host can be used to display array elements in PowerShell, it is not the best tool for presenting data in a table format. PowerShell provides other cmdlets like Format-Table and the ability to create custom objects that offer much more flexibility and power for displaying arrays as tables.

In this PowerShell tutorial, I have explained how to format an array as table in PowerShell and PowerShell Write-Host Array as a Table with examples.

You may also like: