0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Export Data to HTML Report

Use ConvertTo-Html to convert data to HTML and Out-File to save it, like Get-Process | ConvertTo-Html | Out-File report.html.
📋

Examples

InputGet-Process | ConvertTo-Html | Out-File report.html
OutputCreates an HTML file named report.html listing all running processes.
Input$data = Get-Service | Select-Object Name, Status; $data | ConvertTo-Html -Property Name, Status | Out-File services.html
OutputGenerates services.html showing service names and their status in a table.
Input'' | ConvertTo-Html | Out-File empty.html
OutputCreates an empty HTML file with basic HTML structure but no data.
🧠

How to Think About It

To export data to an HTML report in PowerShell, first gather the data you want to show, then convert it to HTML format using ConvertTo-Html. Finally, save the HTML content to a file with Out-File. This process turns raw data into a readable web page.
📐

Algorithm

1
Get the data you want to report on (e.g., processes, services).
2
Use <code>ConvertTo-Html</code> to transform the data into HTML format.
3
Optionally customize the HTML with properties or styles.
4
Save the HTML output to a file using <code>Out-File</code>.
5
Open the saved HTML file in a browser to view the report.
💻

Code

powershell
Get-Process | ConvertTo-Html -Property Id, ProcessName, CPU -Title 'Process Report' | Out-File process_report.html
Write-Output 'HTML report saved as process_report.html'
Output
HTML report saved as process_report.html
🔍

Dry Run

Let's trace creating an HTML report of processes through the code

1

Get-Process

Retrieves a list of running processes with properties like Id, ProcessName, CPU.

2

ConvertTo-Html

Transforms the process list into an HTML table with columns Id, ProcessName, CPU and adds a title.

3

Out-File

Saves the HTML content into the file process_report.html on disk.

StepActionResult
1Get-ProcessList of processes with Id, ProcessName, CPU
2ConvertTo-HtmlHTML string with table and title
3Out-FileFile process_report.html created
💡

Why This Works

Step 1: Gather Data

The Get-Process command collects the data you want to report on, like running processes.

Step 2: Convert Data to HTML

ConvertTo-Html changes the data into an HTML table format, making it easy to view in a browser.

Step 3: Save HTML to File

Out-File writes the HTML content to a file, creating a report you can open anytime.

🔄

Alternative Approaches

Using Select-Object for Custom Columns
powershell
Get-Service | Select-Object Name, Status | ConvertTo-Html -Title 'Service Status' | Out-File services_report.html
Write-Output 'Service report saved as services_report.html'
This method lets you pick specific columns before converting to HTML for a cleaner report.
Adding CSS Styling Inline
powershell
$style = '<style>table {border-collapse: collapse;} th, td {border: 1px solid black; padding: 5px;}</style>'
Get-Process | ConvertTo-Html -Head $style -Title 'Styled Process Report' | Out-File styled_report.html
Write-Output 'Styled HTML report saved as styled_report.html'
Embedding CSS styles improves the look of the HTML report without external files.
Exporting to HTML with Export-CliXml (Legacy)
powershell
Get-Process | Export-CliXml -Path process.xml
Write-Output 'Data saved as XML, not HTML, for later import or processing'
This saves data in XML format, not HTML, useful for data exchange but not for direct viewing.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each item in the data list once to convert it to HTML, so time grows linearly with data size.

Space Complexity

The HTML output is stored in memory before saving, so space usage grows linearly with the amount of data.

Which Approach is Fastest?

Using ConvertTo-Html directly on selected properties is fastest and simplest; adding CSS or extra processing adds minor overhead.

ApproachTimeSpaceBest For
Direct ConvertTo-HtmlO(n)O(n)Quick reports with default styling
Select-Object + ConvertTo-HtmlO(n)O(n)Custom columns, cleaner output
ConvertTo-Html with CSSO(n)O(n)Styled, user-friendly reports
Export-CliXmlO(n)O(n)Data exchange, not HTML viewing
💡
Use -Property with ConvertTo-Html to include only the columns you want in your report.
⚠️
Forgetting to use Out-File after ConvertTo-Html results in HTML output only shown in the console, not saved to a file.