Creating HTML Reports (Part 1 – Creating HTML)

by Aug 9, 2017

To turn PowerShell results in a HTML report, simply pipe the results to ConvertTo-Html, then save the result to file. So in its most basic form, it would look like the code below. It creates a report with all event log system errors that occurred in the past 48 hours:

#requires -Version 2.0

# store report here
$Path = "$env:temp\eventreport.htm"
# set the start date
$startDate = (Get-Date).AddHours(-48)

# get data and convert it to HTML
Get-EventLog -LogName System -EntryType Error -After $startDate |
  ConvertTo-Html |
  Set-Content -Path $Path

# open the file with associated program
Invoke-Item -Path $Path 

However, the resulting report would not only look ugly but also contain a lot of useless information. So the first step in refinement would be to select the properties (columns) you want to see in your report. Simply add Select-Object to your code:

#requires -Version 2.0

$Path = "$env:temp\eventreport.htm"
$startDate = (Get-Date).AddHours(-48)

Get-EventLog -LogName System -EntryType Error -After $startDate |
  # select the properties to be included in your report
  Select-Object -Property EventId, Message, Source, InstanceId, TimeGenerated, ReplacementStrings, UserName |
  ConvertTo-Html |
  Set-Content -Path $Path

Invoke-Item -Path $Path

Twitter This Tip! ReTweet this Tip!