How to Convert String to HTML Table in PowerShell?

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:

  1. We create an array of city names.
  2. We use ForEach-Object to create custom objects with City and Length properties for each city.
  3. We convert these custom objects to an HTML table using ConvertTo-Html, specifying the City and Length properties.
  4. 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:

powershell convert string to html table

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:

convert string to html table using PowerShell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.