Recently, one of my team members struggled to convert a string to an HTML table in PowerShell. I suggested different methods. In this tutorial, I will explain how to convert a string to an HTML table in PowerShell using various methods with examples.
To convert a string array to an HTML table in PowerShell, you can use the ConvertTo-Html cmdlet. For example, if you have an array of city names like @(“New York”, “Los Angeles”, “Chicago”, “Houston”, “Phoenix”), you can pipe this array to ConvertTo-Html and specify the properties you want to include, such as Length and ToUpper. Finally, use Out-File to save the HTML output to a file, creating a structured and readable HTML table from your string array.
Convert String to HTML Table in PowerShell
HTML tables are a great way to display data in a structured format in PowerShell. They are easy to read, styled with CSS, and compatible with web browsers. By converting strings to HTML tables, you can create dynamic reports that can be shared and viewed easily.
Basic Syntax for ConvertTo-Html
The ConvertTo-Html cmdlet in PowerShell is designed to convert .NET objects into HTML that can be displayed in a web browser. Here’s the basic syntax:
ConvertTo-Html [-InputObject] <PSObject> [-Property <Object[]>] [-Head <String>] [-Body <String>] [-PostContent <String>] [-PreContent <String>] [-Title <String>] [<CommonParameters>]Parameters
- -InputObject: Specifies the objects to be converted into HTML.
- -Property: Specifies the properties of the object to include in the HTML table.
- -Head: Adds HTML content to the head of the HTML document.
- -Body: Adds HTML content to the body of the HTML document.
- -PostContent: Adds HTML content after the body.
- -PreContent: Adds HTML content before the body.
- -Title: Specifies the title of the HTML document.
Now, let me show you some examples.
Check out Convert a String to an Array of Characters in PowerShell
Example 1: Convert a Simple String Array to HTML Table
Let’s start with a simple example where we convert an array of strings into an HTML table in PowerShell. Suppose we have an array of city names in the USA:
$cities = @("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")
$htmlTable = $cities | ConvertTo-Html -Property Length, ToUpper
$htmlTable | Out-File -FilePath "C:\MyFolder\CitiesReport.html"In this example, we create an array of city names and then pipe it to ConvertTo-Html. We specify the properties Length and ToUpper to include in the HTML table. Finally, we output the HTML table to a file.
Here is another example:
# Array of city names
$cities = @("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")
# Create custom objects with City and Length properties
$cityObjects = $cities | ForEach-Object {
[PSCustomObject]@{
City = $_
Length = $_.Length
}
}
# Convert the custom objects to an HTML table
$htmlTable = $cityObjects | ConvertTo-Html -Property City, Length -Title "City Names and Lengths"
# Output the HTML table to a file
$htmlTable | Out-File -FilePath "C:\MyFolder\CitiesReport.html"In this script:
- We create an array of city names.
- We use
ForEach-Objectto create custom objects withCityandLengthproperties for each city. - We convert these custom objects to an HTML table using
ConvertTo-Html, specifying theCityandLengthproperties. - Finally, we save the HTML table to a file using
Out-File.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Read Array Contains in PowerShell
Example 2: Convert a Custom Object to HTML Table
In many cases, you’ll be working with custom objects rather than simple strings in PowerShell. Here’s how you can convert a custom object to an HTML table:
$employees = @(
[PSCustomObject]@{Name="John Doe"; Position="Manager"; Location="New York"},
[PSCustomObject]@{Name="Jane Smith"; Position="Developer"; Location="Los Angeles"},
[PSCustomObject]@{Name="Emily Davis"; Position="Designer"; Location="Chicago"}
)
$htmlTable = $employees | ConvertTo-Html -Property Name, Position, Location -Title "Employee Report"
$htmlTable | Out-File -FilePath "C:\MyFolder\EmployeeReport.html"Here, we create an array of custom objects representing employees. We then convert this array into an HTML table, specifying the properties Name, Position, and Location. The -Title parameter adds a title to the HTML document.
Here is the exact output in the screenshot below:

Example 3: Adding Custom HTML Content
You can also add custom HTML content before and after the table. This is useful for adding headers, footers, or additional styling.
$preContent = "<h1>Employee Report</h1><p>Generated on $(Get-Date)</p>"
$postContent = "<footer>Report generated by PowerShell</footer>"
$htmlTable = $employees | ConvertTo-Html -Property Name, Position, Location -PreContent $preContent -PostContent $postContent
$htmlTable | Out-File -FilePath "C:\Reports\AdvancedEmployeeReport.html"In this example, we add a header and a footer to our HTML document using the -PreContent and -PostContent parameters.
Conclusion
Here, I explained how to convert strings and objects to HTML tables in PowerShell using ConvertTo-Html cmdlet. This way, you can create dynamic and readable reports in PowerShell.
I hope you found this tutorial helpful. If you have any questions or need further assistance, feel free to leave a comment below. I will try to reply as soon as possible.
You may also like the following tutorials:
- Convert an Array to a String in PowerShell
- Import CSV to Array in PowerShell
- Format an Array as an HTML Table in PowerShell
- Create an HTML Table from Variables 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.