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

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.

Latest posts by Adam Bertram (see all)

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.

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

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

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.

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.

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.

HTML table with colors

HTML table with colors

Getting the output into HTML opens up a wide range of possibilities. Check out what else ConvertTo-Html can do and, if you're not a web developer, begin searching online for snippets to add to your reports. Remember that even though the resource might not talk about PowerShell, you can use practically any CSS to style these reports in any way you choose.

Win the monthly 4sysops member prize for IT pros

Share
7+

Users who have LIKED this post:

  • avatar
  • avatar
  • avatar

Related Posts

7 Comments
  1. Allan 10 months ago

    Very interesting and informative.  I didn't know that Powershell could do that.

    7+

    Users who have LIKED this comment:

    • avatar
    • Daniel 9 months ago

      It's a matter not of what PowerShell can do... lol but what it can't! 🙂

      0

  2. Riggzie 5 months ago

    So I am searching all over the web... high and low... I am new to powershell but have a script that does run on a machine and gather a ton of information. I am trying to figure out how to get my output to be html instead of txt. All of my items are listed out as variables and all examples I see using the convert-html all use an item that has tabled items.

    so curious if you could help me out. How can I create an html out of just variables. I will show you my code.. well an excerpt as we don't need it all...

    I have some parts of the script have 4 or more rows and some with just 2. so here is an easy one (hopefully so I can get started)

    (will create a txt file with Internet Explorer Version: 11.1358.14393.0)

    $basedir = "c:\PCReports\"
    $today   = (Get-Date -UFormat %B-%d-%Y.%H)
    $file   = (Get-Date -UFormat %B-%d-%Y@%H.%M)
    $location = New-Item -Path $basedir -Type Directory -Name $today
    $LCSSettingsVerification = "\LCS_Machine_Info_Report-" + $file + ".txt"
    $FileSave = $basedir + $today + $LCSSettingsVerification

    $myIEVersion =  (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Internet Explorer').svcVersion
    if (!$myIEVersion) {$myIEVersion =  (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Internet Explorer').Version}
    write-output ('Internet Explorer Version: ' + $myIEVersion) |Out-File $FileSave -Append

     

    0

    • yan 4 months ago

      You could create a here string, put your variables into a html table, then output the here string to a html file

      0

  3. Jim 4 months ago

    Can you use external style sheets?

    1+

  4. KK 3 months ago

    Hi,

    Very informative, Thanks for sharing the knowledge.

    Please help me in clarifying my doubt.

    I have a text file (with some data) which I want to convert to HTML with formatting.

    This is what i am trying

    Get-Content Data.txt | ConvertTo-html | Out-File Data.html.

    But I am not getting the desired output. Instead of getting the content of the Data.file in Html format I am getting the path of the Data.txt.

    I am new to this, please help me out here.

    Regards,

    KK

    0

  5. uday 1 day ago

    HI ,

    how to highlight the last row in a column using the same logic.

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account