The ConvertTo-Html cmdlet allows you to display PowerShell output in a presentable way. In this post, I will show you how.

Let's say you've developed this excellent script that pulls all kinds of information from various sources that save you and your team tons of time. This script pulls information from Active Directory, some HR database, your VMware/Hyper-V nodes and so on. How it correlates all of that information is really a huge time-saver for you.

You present it to a non-technical manager; he looks at the text output and his eyes glaze over. He can't understand the default PowerShell console output. Regardless of how useful you think the information is, if you can't convey that usefulness to non-technical people, it probably won't work.

PowerShell, as we know, is a command-line tool. It takes input from the command line and sends output to the console in text form. It doesn't have to be this way though. By using a built-in cmdlet and a little HTML/CSS wizardry, you can build reports in HTML that have tables, color and a whole lot more. To do this requires using the ConvertTo-Html cmdlet.

By default, when PowerShell returns a table of information, it will have multiple columns and rows.

Default table output

Default table output

This is typically a useful way to present the information. However, it's not necessarily visually appealing. PowerShell provides a built-in cmdlet called ConvertTo-Html. This takes objects as input and converts each of them to an HTML web page. To use this, just take the output and pipe it directly to ConvertTo-Html. The cmdlet will then return a big string of HTML.

Get-PSDrive | ConvertTo-Html

Since this big string of output isn't too useful, let's capture it to a file using the Out-File cmdlet.

Get-PSDrive | ConvertTo-Html | Out-File -FilePath PSDrives.html

After doing this, check out the PSDrives.html file that this generated.

Invoke-Expression C:\PSDrives.html
HTML output

HTML output

Notice it shows nearly the exact same output as the console, only in HTML. What did you expect, anyway? But at this point, we now have the opportunity to make it look better. First, let's limit the number of properties to only those that show up when running Get-PSDrive from the console. Those properties are Name, Used, Provider, Root and CurrentLocation. You can limit the output to these properties by using the Property parameter of ConvertTo-Html.

Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation | Out-File -FilePath PSDrives.html
Better HTML output

Better HTML output

Now it looks like the native console output. We can do much better though. I find it hard to differentiate between the rows since they're so close together. Let's add a border around the rows and columns. To do that requires knowing a little CSS, which I was able to find online. The ConvertTo-Html cmdlet has a Head parameter that allows you to specify the HTML code that will go into the HEAD element. Below, I'm creating a simple CSS style tag to create a border.

$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header | Out-File -FilePath PSDrives.html
HTML table border

HTML table border

You should now begin to see the possibilities here. Although you'll need to know a little about CSS, you have the ability to make this report look a whole lot better. For my final trick, I'll add a color to the table header values to make them more obvious.

Subscribe to 4sysops newsletter!

$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
HTML table with colors

HTML table with colors

Getting the o