PowerShell Script to Export Data to HTML Report
ConvertTo-Html to convert data to HTML and Out-File to save it, like Get-Process | ConvertTo-Html | Out-File report.html.Examples
How to Think About It
ConvertTo-Html. Finally, save the HTML content to a file with Out-File. This process turns raw data into a readable web page.Algorithm
Code
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'
Dry Run
Let's trace creating an HTML report of processes through the code
Get-Process
Retrieves a list of running processes with properties like Id, ProcessName, CPU.
ConvertTo-Html
Transforms the process list into an HTML table with columns Id, ProcessName, CPU and adds a title.
Out-File
Saves the HTML content into the file process_report.html on disk.
| Step | Action | Result |
|---|---|---|
| 1 | Get-Process | List of processes with Id, ProcessName, CPU |
| 2 | ConvertTo-Html | HTML string with table and title |
| 3 | Out-File | File 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
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'
$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'
Get-Process | Export-CliXml -Path process.xml
Write-Output 'Data saved as XML, not HTML, for later import or processing'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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct ConvertTo-Html | O(n) | O(n) | Quick reports with default styling |
| Select-Object + ConvertTo-Html | O(n) | O(n) | Custom columns, cleaner output |
| ConvertTo-Html with CSS | O(n) | O(n) | Styled, user-friendly reports |
| Export-CliXml | O(n) | O(n) | Data exchange, not HTML viewing |
-Property with ConvertTo-Html to include only the columns you want in your report.Out-File after ConvertTo-Html results in HTML output only shown in the console, not saved to a file.