Outputting HTML Reports

by Jan 5, 2009

PowerShell can export results as HTML. Simply pipe the results to ConvertTo-HTML and save the result in a file. When you do that, it is wise to use Select-Object to first limit the object properties to only those you want to see in your report, otherwise your HTML table gets huge.

Get-Service | Select-Object Status, Name, DisplayName | ConvertTo-HTML | Out-File report.htm
.report.htm

The resulting report is a bit ugly, though. With the help of the optional parameters, -title, -body and -head, you can add colors, fonts and other design information. The most important customizing parameter is -head, allowing you to define styles for the different HTML elements in your report.

$head = @'<style>
BODY{font-family:Verdana; background-color:lightblue;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#FFCCCC}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:yellow}
</style>'@

$header = "<H1>Reporting Service Status</H1>"
$title = "Example HTML Output"

Get-Service |
Select-Object Status, Name, DisplayName |
ConvertTo-HTML -head $head -body $header -title $title |
Out-File report.htm
.report.htm