ConvertTo-Html

PowerShell Cmdlet Deep Dive: ConvertTo-Html

Welcome back to Wahmans PowerShell blog! Today, we’re diving into a super useful cmdlet that helps you quickly turn your PowerShell data into beautiful, browser-friendly reports: ConvertTo-Html.

The official Microsoft documentation describes it as:

Converts .NET objects into HTML that can be displayed in a Web browser.

And that’s exactly what it does! Whether you’re looking to share system data with coworkers or generate quick reports for your own use, ConvertTo-Html is a handy tool to have in your toolbox.

Getting Started

ConvertTo-Html takes the output of any cmdlet or object and turns it into HTML markup. You can then redirect this HTML to a file or display it in a browser. Let’s walk through four examples—starting from beginner and moving toward more advanced use cases.

Example 1: Basic Table from Get-Process

Let’s create a simple HTML table of running processes:

Get-Process | ConvertTo-Html | Out-File processes.html

This command fetches all running processes, converts the data into an HTML table, and saves it to processes.html. Open the file in a browser to view the result.

Example 2: Adding a Custom Title and Body Tags

You can enhance the HTML with -Title and -Body parameters to add some context:

Get-Service | ConvertTo-Html -Title "Service Report" -PreContent "<h2>List of Windows Services</h2>" | Out-File services.html

This will generate an HTML file with a custom title and a heading before the table.

Example 3: Styling the Output with CSS

Want to make your reports look a little nicer? You can inject CSS styling using the -Head parameter:

$style = @"
<style>
    body { font-family: Arial; background-color: #f0f0f0; }
    table { border-collapse: collapse; width: 100%; }
    th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
    th { background-color: #333; color: white; }
</style>
"@

Get-EventLog -LogName System -Newest 10 |
    ConvertTo-Html -Title "System Event Log" -Head $style |
    Out-File eventlog.html

This HTML file will now have a clean, styled table displaying the latest system events.

Example 4: Creating an HTML Dashboard with Multiple Tables

Let’s finish with a more advanced example. What if you want to stack multiple reports in one HTML file?

$systemInfo = Get-ComputerInfo | ConvertTo-Html -Fragment -PreContent '<h2>System Information</h2>'
$diskInfo = Get-PSDrive | ConvertTo-Html -Fragment -PreContent '<h2>Disk Usage</h2>'

$style = @"
<style>
    body { font-family: Segoe UI; background-color: #ffffff; padding: 20px; }
    table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
    th, td { border: 1px solid #ddd; padding: 8px; }
    th { background-color: #f4f4f4; }
</style>
"@

$fullHtml = ConvertTo-Html -Title "System Dashboard" -Head $style -Body ($systemInfo + $diskInfo)
$fullHtml | Out-File dashboard.html

This combines multiple tables into a single dashboard-style report. Perfect for daily server health checks or documentation.

Conclusion

ConvertTo-Html is a flexible cmdlet that can turn ordinary PowerShell output into formatted, presentable reports with minimal effort. From quick diagnostics to full HTML dashboards, it’s a strong ally for any sysadmin or scripter.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *