How to Create an HTML Table from Variables in PowerShell?

You can create an HTML table from variables in PowerShell to generate dynamic and readable reports. Whether you’re working with data from a SQL query, a CSV file, or a collection of objects, it is easy to convert this information into an HTML table in PowerShell. In this tutorial, I will explain how to create an HTML table from variables in PowerShell using different methods with examples.

To create an HTML table from variables in PowerShell, you can use the ConvertTo-Html cmdlet. Start by defining your data as an array of custom objects, then pipe this array into ConvertTo-Html specifying the properties you want to include. Finally, save the HTML output to a file using Out-File. For example:

$data = @(
    [PSCustomObject]@{Name="John Doe"; Age=30; City="New York"},
    [PSCustomObject]@{Name="Jane Smith"; Age=25; City="Los Angeles"}
)
$html = $data | ConvertTo-Html -Property Name, Age, City -Title "User Data"
$html | Out-File -FilePath "C:\Reports\UserData.html"

This will generate an HTML file with a table containing your specified data.

Create an HTML Table from Variables in PowerShell

PowerShell provides various methods to create an HTML table from a variable.

But let me first show you the basic syntax.

The core cmdlet for converting objects to HTML in PowerShell is ConvertTo-Html. Here’s a basic example of how to use it:

$data = @(
    [PSCustomObject]@{Name="John Doe"; Age=30; City="New York"}
    [PSCustomObject]@{Name="Jane Smith"; Age=25; City="Los Angeles"}
)

$html = $data | ConvertTo-Html -Property Name, Age, City -Title "User Data"
$html | Out-File -FilePath "C:\MyFolder\UserData.html"

In this example:

  • We create a custom object array $data.
  • We convert this array to HTML using ConvertTo-Html.
  • We save the HTML output to a file using Out-File.

Here is the exact output in the screenshot below:

powershell create html table from variables

This is just a basic example, but sometimes, you may want to add some styling to it. Here is a complete and detailed example of how to add styling through CSS and formatting.

$data = @(
    [PSCustomObject]@{Name="Michael Johnson"; Age=45; City="Chicago"}
    [PSCustomObject]@{Name="Emily Davis"; Age=32; City="Houston"}
    [PSCustomObject]@{Name="David Brown"; Age=29; City="Phoenix"}
)

$html = $data | ConvertTo-Html -Property Name, Age, City -Title "User Information" -PreContent "<h1>User Information Report</h1>" -PostContent "<footer>Generated on $(Get-Date)</footer>"

# Adding some CSS for better presentation
$style = @"
<style>
    table { border-collapse: collapse; width: 100%; }
    th, td { border: 1px solid black; padding: 8px; text-align: left; }
    th { background-color: #f2f2f2; }
</style>
"@

$html = $style + $html
$html | Out-File -FilePath "C:\MyFolder\UserInformation.html"

In this example:

  • We add a header and footer using -PreContent and -PostContent.
  • We include CSS styles to make the table look better.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

create html table from variables using PowerShell

Now, let me show you other methods to create an HTML table from a variable in PowerShell.

Check out Format an Array as an HTML Table in PowerShell

Method 1: Using Custom HTML

Sometimes, you might need more control over the HTML structure. Here’s how you can manually create an HTML table in PowerShell.

$data = @(
    [PSCustomObject]@{Name="Sarah Parker"; Age=37; City="San Francisco"}
    [PSCustomObject]@{Name="James Wilson"; Age=40; City="Dallas"}
)

$html = @"
<html>
<head>
    <title>User Data</title>
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid black; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h1>User Data Report</h1>
    <table>
        <tr><th>Name</th><th>Age</th><th>City</th></tr>
"@

foreach ($item in $data) {
    $html += "<tr><td>$($item.Name)</td><td>$($item.Age)</td><td>$($item.City)</td></tr>"
}

$html += @"
    </table>
    <footer>Generated on $(Get-Date)</footer>
</body>
</html>
"@

$html | Out-File -FilePath "C:\MyFolder\CustomUserData.html"

In this method:

  • We manually build the HTML string.
  • This allows for more customization and control over the final output.

Check out Create a Table with Headers in PowerShell

Method 2: Using ConvertTo-Html with Parameters

You can also use various parameters with ConvertTo-Html to customize the output in PowerShell.

Here is an example and the complete PowerShell script.

$data = @(
    [PSCustomObject]@{Name="Laura Martinez"; Age=28; City="Miami"}
    [PSCustomObject]@{Name="Robert Garcia"; Age=33; City="Seattle"}
)

$html = $data | ConvertTo-Html -Property Name, Age, City -Title "Employee Data" -As Table 

$header = "<html><head><title>Employee Data</title></head><body><h1>Employee Data Report</h1>"
$footer = "<footer>Generated on $(Get-Date)</footer></body></html>"

$finalHtml = $header + $html + $footer
$finalHtml | Out-File -FilePath "C:\MyFolder\EmployeeData.html"

In this method:

  • We use the -As Table and -Fragment parameters to format the HTML.
  • We manually add the header and footer.

Here is the exact output in the screenshot below:

How to create html table from variables using PowerShell

Conclusion

In this tutorial, I explained how to create an HTML table from a variable in PowerShell using ConvertTo-HTML cmdlets and how to manually build HTML, etc. Try these methods, and let me know if you still have any questions in the comment below.

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.