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 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.

avataravataravataravatar
20 Comments
  1. Allan 6 years ago

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

    avatar
    • Daniel 6 years ago

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

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

     

    • yan 6 years ago

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

  3. Jim 6 years ago

    Can you use external style sheets?

    • Gaz 5 years ago

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

      convertto-html -head $head -body $body

  4. KK 6 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

  5. uday 6 years ago

    HI ,

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

  6. Dimiter 5 years 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.

     

  7. SG 5 years ago

    Thanks Adam!

  8. Gautam 5 years 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

    }

  9. havingacode 4 years ago

    Hi,

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

    Thanks

    • @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.

      Get-PSDrive|Select-Object -Property Name,Used,Free

  10. cloudinmyhead 4 years 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.

    • 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.

  11. Kapil 4 years 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?

  12. Gabbark 3 years ago

    How to get html file content as a mail body? or output of Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header | Out-File -FilePath PSDrives.html

    • Leos Marek (Rank 4) 3 years ago

      $body = Get-PSDrive | ConvertTo-Html -Property Name,Used,Provider,Root,CurrentLocation -Head $Header

      Send-MailMessage -body $body -bodyasHTML:$true

      add other required parameters to Send-MailMessage of course. 🙂

  13. Greg 3 years ago

    If you are going to execute this:

    Invoke-Expression C:\PSDrives.html

    Then you need to change this

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

    to this

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

    if you expect it to be in the same place, just my 2 cents.

  14. Vipin Sharma 3 years ago

    lovely.. you helped me..!!

    Thanks a lot..!!yessmiley

    avatar

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2023

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