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 $myArrayOutput:
Item1 Item2 Item3Format 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-TableHowever, 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 -AutoSizeIn this example, we create an array of hash tables, each representing a person with Name, Age, 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 ArchitectI executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

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 -AutoSizeThis will output:
Name Job
---- ---
Alice Engineer
Bob Designer
Charlie ArchitectCreate 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 ElementOutput:
Element
-------
Item1
Item2
Item3You can see the output in the screenshot below:

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-TableOutput:
Item Description Value
---- ----------- -----
Item1 Description1 Value1
Item2 Description2 Value2
Item3 Description3 Value3Conclusion
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:
- How to Sum All Numbers in an Array in PowerShell?
- Join an Array into a String in PowerShell
- Check if an Array Contains More than One Value in PowerShell
- Reverse An Array in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.