ConvertTo-HTML Tips and Tricks

Posted on July 25, 2017 by Jeff Hicks in PowerShell with

Everybody loves a nice HTML report. Am I right? Creating these reports in PowerShell is not that difficult. I thought I would offer up some tips and tricks you may not be aware of, including a few that I use all the time when I am creating reports. Some of these are documented in the help for ConvertTo-HTML. You do read the help, correct? Remember that ConvertTo-HTML does not create an actual file, just the HTML code. This is good for us. This means that we can modify the HTML on the fly and save the file when finished.

 

 

Be Selective

The first, and perhaps most important tip, is to be selective. Let’s take a simple command and output like this one.

A simple PowerShell command (Image credit: Jeff Hicks)

A Simple PowerShell Command (Image credit: Jeff Hicks)

You want to create an HTML version of this, so you run a command like this:

When you open the file in your browser, things are not quite what you expected.

Converted output (Image credit: Jeff Hicks)

Converted Output (Image credit: Jeff Hicks)

This is because Convertto-Html, like Export-CSV and Export-Clixml, take the entire object. This is not just the default result you see on the screen. Remember, everything will be treated as a string. In my example, if I want a similar HTML file, I will have to recreate the output with Select-Object. This might require piping the original result to Get-Member to discover the “real” property names.

I always test without converting first.

Recreating output (Image credit: Jeff Hicks)

Recreating Output (Image credit: Jeff Hicks)

I get a list instead of a table but at least the properties are correct. Let’s recreate the HTML file by piping the above to Convert-HTML and then Out-File overwriting the original and useless file.

The revised file (Image credit: Jeff Hicks)

The Revised File (Image credit: Jeff Hicks)

This is much better compared to what I had originally. Although, since everything is being treated as a string, I can format the numeric values to look more like the original PowerShell output.

I also went and added a title, so I get something more useful than HTML TABLE.

A better formatted report (Image credit: Jeff Hicks)

A Better Formatted Report (Image credit: Jeff Hicks)

Use Pre/Post Content

We are starting to build something with a bit more visual appeal. This leads me to another suggestion, take advantage of the pre and post content parameters.

When you pipe something to ConvertTo-HTML, the cmdlet generally creates a table. But you can insert additional HTML directives for things to put in before the main code and after. You can use as much html code as you want.

I added an intro element using the H1 tag to display the computer name. Following the content, I am inserting the current date and time formatted with the H5 heading and italic tags.

Report with pre and post content (Image credit: Jeff Hicks)

Report with Pre and Post Content (Image credit: Jeff Hicks)

Of course, you can always specify the path to a CSS file.

With a style sheet (Image credit: Jeff Hicks)

With a Style Sheet (Image credit: Jeff Hicks)

If you want to play along at home, this is the CSS code I am using:

Sponsored

I am using a file on my hard drive. This means that if anyone else were to look at the file, they would not get the same formatting. The solution is to use a CSS file path that everyone can reach. Check back later for the next article in this series.

Tagged with , ,

Register for this Webinar

How Replication Supports Your Company’s RTOs & RPOs
Join us for this free webinar

Can you have your workloads running within the agreed RTOs? Join this webinar with expert speakers from Veeam to exceed business objectives with an RPTO<15 min for ALL of your application and data.

Thursday, December 14, 2017 at 11 a.m EST