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 Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

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.

Join the 4sysops PowerShell group!

22+

Users who have LIKED this post:

  • avatar
  • avatar
  • avatar
  • avatar
Share
16 Comments
  1. Allan 3 years ago

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

    11+

    Users who have LIKED this comment:

    • avatar
    • Daniel 3 years ago

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

      1+

  2. Riggzie 2 years 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

     

    1+

    • yan 2 years ago

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

      1+

  3. Jim 2 years ago

    Can you use external style sheets?

    2+

    • Gaz 1 year ago

      $head=  <link rel="stylesheet" href="styles.css">

      convertto-html -head $head -body $body

      1+

  4. KK 2 years 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

    4+

  5. uday 2 years ago

    HI ,

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

    3+

  6. Dimiter 1 year ago

    Excel Work Sheet to HTML from PowerShell. It creates folder which I don’t want and also inserts Java script in the output also not needed. IF I saved from Excel then is the way I expect it be. How can it be done with Powershell?

    I've tried:

    $Tmp5 = “Test.htm” $xlHtml = 44 # Creates folder Test_files

    $ws.SaveAs($Tmp5,$xlHtml)
    $xlHtml = 45 # doesn’t create files but wrong format Java etc.

     

    1+

  7. SG 1 year ago

    Thanks Adam!

    1+

  8. Gautam 1 year ago

    Hi Adam,

    Sorry to bug you, I used your teachings and created Table from powershell. Would you please be able to help me with "creating Charts" from those tables as well ? My tables are stored in $Test variable.

    Small Example below:

    function GetSummary {

    $a=Get-MsolDomain | select name,capabilities,status | ConvertTo-Html

    # Creating the HTML Page
    $Html = @"
    <!DOCTYPE html>
    <head>
    <title>Test Page</title>
    <style>
    body {
    font-family: "Courier New";
    font-size: 10px;
    line-height: 2px
    }
    table, th, td {
    white-space: nowrap;
    border: 1px solid #25724F;
    text-align: center;
    border-collapse: collapse;
    padding: 2px;
    }
    th {
    background-color:#25724F;
    color: white;
    }
    </style>
    </head>
    <body>
    <br>
    Available Domains:
    <table class="4sysops">
    <tr>
    <td class="4sysops-1" colspan="6">$a</td>
    </tr>
    </table>

    "@

    Send-Mailmessage -from gautam.verma@domain.com  -body $html -BodyAsHtml

    }

    1+

  9. havingacode 5 months ago

    Hi,

    Any idea why it goes from declaring Used and Free in  GB to B when you invoke ConvertTo-HTML?

    Thanks

    0

    • Luc Fullenwarth 5 months ago

      @havingacode

      This is a hard-coded functionality specific to the "default view".
      The real value of the property is in Bytes.
      When you pipe the result from the Get-PSDrive cmdlet to Select-String or Format-Table you get the number in Bytes too.

      0

  10. cloudinmyhead 2 months ago

    I really like this article and it works well with PowerShell built in commands but I can't get it to work for output from an EXE file that is called from PowerShell.

    In my case I have an application ($EXE) and an output file ($OutputFile) that has a 4 lines output. I cannot get the output to be in HTML though.

    This works for me but it output as plain text
    Start-Process -FilePath $EXE -Wait -WindowStyle Hidden -RedirectStandardOutput $OutputFile

    For some reason ConvertTo-Html will not work for output from an command line exe.

    0

    • Swapnil Kambli 2 months ago

      The result of command 'convert-to-html' would depend on the format of your file. You may need to add some parsing/trimming logic to get the file into a delimited format.

      0

  11. Kapil 4 weeks ago

    $process = Import-Csv C:\Temp\Process.csv
    foreach ($proc in $process)
        {
            $result = Get-Process -Name $proc.Name
            Write-Host $proc.name,$result.Handles,$proc.Value
        }
        

    For above command I can see the output on screen but how can get the same in CSV or HTML?

    0

Leave a reply

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

*

© 4sysops 2006 - 2019

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account