One of my team members recently asked me to create an HTML table in PowerShell to generate reports. There are different methods to do this. In this tutorial, I will explain how to create an HTML table in PowerShell with examples.
To create an HTML table in PowerShell, you can use the ConvertTo-Html cmdlet, which transforms objects into HTML format. Start by defining your data as a custom object array, then pipe it to ConvertTo-Html specifying the desired properties. For example, to list employees’ names and job titles, use:
$employees = @(
[PSCustomObject]@{Name="John Doe"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Smith"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Robert Johnson"; JobTitle="Business Analyst"}
)
$employees | ConvertTo-Html -Property Name, JobTitle -Title "Employee List" -PreContent "<h1>Employee List</h1>" | Out-File "C:\MyFolder\EmployeeList.html"This command generates an HTML file displaying the data in a structured table.
Create an HTML Table in PowerShell
HTML tables are a great way to organize data. They allow you to display information in rows and columns, making it easy to read and analyze. In PowerShell, it is easy to convert data into HTML tables, which can be particularly useful for generating reports that can be viewed in a web browser or sent via email.
There are various methods to create an HTML table in PowerShell. Let me show you each method with examples.
Syntax to Create HTML Tables
The ConvertTo-Html cmdlet in PowerShell is primarily used to convert objects into HTML. Here’s the basic syntax:
ConvertTo-Html [-InputObject] <PSObject> [-Property <Object[]>] [-Head <String>] [-Body <String>] [-Title <String>] [-PreContent <String>] [-PostContent <String>] [-Fragment] [<CommonParameters>]Here are the parameters.
- InputObject: The object to be converted into HTML.
- Property: Specifies which properties of the object to include in the HTML table.
- Head: Specifies the content to include in the
<head>section of the HTML. - Body: Specifies the content to include in the
<body>section of the HTML. - Title: Specifies the title of the HTML document.
- PreContent: Content to include before the HTML table.
- PostContent: Content to be included after the HTML table.
- Fragment: Generates an HTML fragment.
Check out Create an HTML Table from Variables in PowerShell
Example 1: Create A Basic HTML Table in PowerShell
Let’s start with a simple example. Suppose we have a list of employees, and we want to display their names and job titles in an HTML table.
$employees = @(
[PSCustomObject]@{Name="John Doe"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Smith"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Robert Johnson"; JobTitle="Business Analyst"}
)
$employees | ConvertTo-Html -Property Name, JobTitle -Title "Employee List" -PreContent "<h1>Employee List</h1>" | Out-File "C:\MyFolder\EmployeeList.html"In this example, we create a custom object array $employees and convert it to an HTML table using ConvertTo-Html. We include the properties Name and JobTitle, and add a title and a header.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Example 2: Customize HTML Table with CSS
We can add some CSS styling to make our HTML table more visually appealing. Here is the complete PowerShell script.
$employees = @(
[PSCustomObject]@{Name="John Doe"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Smith"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Robert Johnson"; JobTitle="Business Analyst"}
)
$style = @"
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid black; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
"@
$employees | ConvertTo-Html -Property Name, JobTitle -Head $style -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 head section using the -Head parameter.
You can see the output in the screenshot below:

Check out Convert String to HTML Table in PowerShell
Example 3: Create HTML Tables from Variables
Sometimes, you might need to create an HTML table in PowerShell from variables. Here’s an example and the complete PowerShell script
$city = "New York"
$population = 8419000
$area = 468.9
$data = [PSCustomObject]@{
City = $city
Population = $population
Area = $area
}
$data | ConvertTo-Html -Title "City Information" -PreContent "<h1>City Information</h1>" | Out-File "C:\MyFolder\CityInfo.html"Here, we create a custom object $data with variables and convert it to an HTML table.
Here is the exact output in the screenshot below:

Read Format an Array as an HTML Table in PowerShell
Example 4: Using ConvertTo-Html with ForEach-Object
If you need to iterate over a collection and generate an HTML table in PowerShell, you can use ForEach-Object:
$servers = @("Server1", "Server2", "Server3")
$results = @()
$servers | ForEach-Object {
$ping = Test-Connection -ComputerName $_ -Count 1
$results += [PSCustomObject]@{ServerName=$_; Status=$ping.StatusCode}
}
$results | ConvertTo-Html -Property ServerName, Status -Title "Server Status" -PreContent "<h1>Server Status</h1>" | Out-File "C:\MyFolder\ServerStatus.html"In this example, we ping a list of servers and store the results in an array, which we then convert to an HTML table.
Conclusion
In this tutorial, I explained how to create an HTML table in PowerShell using different methods. I explained how to use the ConvertTo-HTML cmdlet and how to add CSS styling to the HTML table in PowerShell.
If you still have questions, feel free to comment below.
You may also like:
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.