PowerShell Problem Solver: Create a Grouped HTML Report with PowerShell

Posted on April 8, 2015 by Jeff Hicks in PowerShell with 0 Comments

I always talk about “objects in the pipeline” when writing or training about PowerShell. We shouldn’t be thinking about trying to parse text or even be too concerned about individual items. Most of the time we can simply let the PowerShell pipeline do all of the work for us. With that said, you can take control, too. One such situation might be when you want to look at the results of PowerShell expression, where it would help to see results grouped together. Taking this scenario a step further, you may want to create an HTML report using grouped output. If you look at help for Convertto-HTML, you won’t see a –Group parameter or anything that looks like it will help. As I’ll show you, it isn’t too difficult to achieve.

First, let’s look at creating a nice formatted group report in the console. We’ll begin with some data.

The variable $data should contain all the files in my Scripts folder. Let’s say I want to create a formatted report that shows files grouped by directory.

With the Format-Table cmdlet, you can specify a property name to group by. I recommend you sort on that same property first as I’ve done.

Create a Grouped HTML Report with PowerShell

This isn’t too difficult because PowerShell has built-in instructions that tell it how to format a table of file objects. What I want next is to duplicate this in an HTML file. To accomplish this, I’ll need an intermediate step to group the data accordingly.

Technically, I didn’t have to sort before grouping. But the result will be that each group of related files is now sorted first by directory and then length,

Each group of related files is now sorted first by directory and then length. (Image Credit: Jeff Hicks)

Each group of related files is now sorted first by directory and then length. (Image Credit: Jeff Hicks)


I now have a new set of objects. The file objects are in the Group property for each grouped entry. To create the HTML report, I will need to use fragments. Instead of piping directly to Convertto-HTML, I’m going to create my own body. I’ll start with a blank string.

Now for the fun part. I’m going to enumerate each grouped entry in $grouped. I’m going to use the Name property as my heading with an H2 HTML tag. Next, I’m going to send the collection of files and create an HTML table fragment. A table is the default, so I didn’t have specify the –As parameter, but I wanted to demonstrate it in case you needed a list instead.

You have to remember that Convertto-HTML will look at any incoming objects and convert all properties, not just the ones you see on the screen. That’s why I’m selecting a few objects first. With the body complete, I’m now ready to create the final document.

I’m adding style from an external CSS file and including a footer of sorts with –PostContent displaying the current date and time. This is what I end up with:

You could follow the same process for any type of object. Because this is likely to be a repeatable process, this is something you want to turn into a PowerShell tool. For those of you panicking thinking you can’t do it, relax. Here’s my version of a tool that I call Out-HTMLReport.

The function is intended to be used at the end of a PowerShell expression is the same way would you use Out-File or Out-Printer. If you look through the function, you’ll see that is a wrapper to Convertto-HTML and uses many of the same parameters. Here’s how you can use it. First, I need some new data.

I have 100 event logs from the System log, and I want to create an HTML report grouped on the Source property. I’ll use this hashtable of parameters to splat to my function.

This makes it easy to create the report.

If you don’t want to reference an external CSS file you can include style in the head section. First, some new data.

Next, define a head section with style.


I’ll use these parameters and create the report:

You can use my function even if you don’t want to group output. I hope you’ve seen what you can accomplish with PowerShell and that it isn’t especially complicated, especially if you read full help and examples for commands like Format-Table and ConvertTo-HTML.


Tagged with , , , , , ,