One of my team members wanted to format an array as an HTML table. I suggested different methods to do so. In this tutorial, I will explain how to format an array as an HTML table using PowerShell with some examples. This is particularly useful for creating reports or sending data via email in a structured format.
To format an array as an HTML table in PowerShell, you can use the ConvertTo-Html cmdlet. First, create an array of hash tables with your data, and then pipe it to ConvertTo-Html specifying the desired properties. For example, $employees | ConvertTo-Html -Property Name, Department, Location -Title “Employee List” | Out-File “C:\Reports\EmployeeList.html” will generate an HTML file with a table containing the specified properties.
PowerShell Format Array as HTML Table
In PowerShell, formatting arrays as HTML tables can help you present data more readably and organizedly. Converting arrays to HTML tables is necessary for presentable formatting, whether generating reports, exporting data, or automating email notifications.
ConvertTo-Html Cmdlet
The ConvertTo-Html cmdlet is primarily used to convert objects to HTML in PowerShell. This cmdlet can take an array and convert it into an HTML table format.
Syntax:
Here is the syntax:
ConvertTo-Html [-InputObject] <psobject> [-Property <Object[]>] [-Head <String[]>] [-Body <String[]>] [-PostContent <String[]>] [-PreContent <String[]>] [-As <String>] [-Fragment] [-CssUri <Uri>] [-Title <String>] [<CommonParameters>]Format-Table Cmdlet
The Format-Table cmdlet is used to format the output of a command as a table with the selected properties of the object in each column.
Syntax:
Here is the syntax:
Format-Table [-Property <Object[]>] [-GroupBy <Object>] [-HideTableHeaders] [-AutoSize] [-Wrap] [-Force] [<CommonParameters>]Check out Convert an Array to a String in PowerShell
Format an Array as an HTML Table in PowerShell Examples
Now, let me show you a few examples of formatting an array an an HTML table in PowerShell.
Example 1: Basic HTML Table
Let’s start with a basic example. Suppose we have an array of hash tables containing information about employees.
$employees = @(
@{Name="John Doe"; Department="HR"; Location="New York"},
@{Name="Jane Smith"; Department="IT"; Location="San Francisco"},
@{Name="Sam Johnson"; Department="Finance"; Location="Chicago"}
)
$employees | ConvertTo-Html -Property Name, Department, Location -Title "Employee List" -PreContent "<h1>Employee List</h1>" | Out-File "C:\MyFolder\EmployeeList.html"In this example, we create an array $employees and convert it to an HTML table using the ConvertTo-Html cmdlet. The resulting HTML file includes a title and a header.
Here is the output you can see in the screenshot below:

Check out Remove Array Element by Index in PowerShell
Example 2: Adding Custom CSS
You can also add custom CSS to style your HTML table in PowerShell. Here’s how:
$css = @"
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid black; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
"@
$employees = @(
@{Name="John Doe"; Department="HR"; Location="New York"},
@{Name="Jane Smith"; Department="IT"; Location="San Francisco"},
@{Name="Sam Johnson"; Department="Finance"; Location="Chicago"}
)
$employees | ConvertTo-Html -Property Name, Department, Location -Head $css -Title "Styled Employee List" -PreContent "<h1>Styled Employee List</h1>" | Out-File "C:\MyFolder\StyledEmployeeList.html"In this example, we define and include a CSS style block in the HTML output using the -Head parameter.
Once you execute the above PowerShell script, you will see the exact output in the screenshot below:

Check out PowerShell where-object in array
Example 3: Formatting with Format-Table
If you prefer to format your data before converting it to HTML, you can use the Format-Table cmdlet.
$employees = @(
@{Name="John Doe"; Department="HR"; Location="New York"},
@{Name="Jane Smith"; Department="IT"; Location="San Francisco"},
@{Name="Sam Johnson"; Department="Finance"; Location="Chicago"}
)
$employees | Format-Table -Property Name, Department, Location | Out-String -Width 120 | ConvertTo-Html -Title "Formatted Employee List" -PreContent "<h1>Formatted Employee List</h1>" | Out-File "C:\MyFolder\FormattedEmployeeList.html"Here, we use Format-Table to format the data and Out-String to convert it to a string before passing it to ConvertTo-Html.
Example 4: Including Additional HTML Content
You can add additional HTML content such as headers, footers, or custom messages.
$preContent = @"
<h1>Employee List</h1>
<p>This report contains the list of employees in various departments.</p>
"@
$postContent = @"
<p>Generated on $(Get-Date)</p>
"@
$employees | ConvertTo-Html -Property Name, Department, Location -PreContent $preContent -PostContent $postContent -Title "Employee List with Additional Content" | Out-File "C:\MyFolder\EmployeeListWithContent.html"In this example, we add a header and a footer to the HTML table using the -PreContent and -PostContent parameters.
Here is the exact output in the screenshot below:

Conclusion
In this tutorial, I explained how to format arrays as HTML tables in PowerShell using different methods, such as ConvertTo-Html and Format-Table cmdlets. I have also explained how to use custom CSS to format an HTML table in PowerShell.
You may also like the following tutorials:
- Convert Object to Array in PowerShell
- Sort an Array of Objects in PowerShell
- PowerShell Format-Table
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.