PowerShell ConvertTo-Html

PowerShell ConvertTo-Html Cmdlet

When testing PowerShell’s ConvertTo-Html cmdlet, the best way of making sense of the output is if you employ Invoke-Item to view the resulting web page.

Topics for PowerShell’s ConvertTo-Html


Introduction to ConvertTo-Html

ConvertTo-Html feeds on input from another PowerShell cmdlet; the output is displayed in a web page.  Your alternatives are to save the data in a text file, in which case you would employ Out-File, do nothing and display the data in the PowerShell ISE, or call for ConvertTo-Html.

Example 1: List Aliases in a HTML file

The purpose of this script is to simply list PowerShell’s aliases in a web page.

# PowerShell ConvertTo-Html Example
Get-Alias | ConvertTo-Html -property Name, Definition |
Out-File aliases.htm
Invoke-Item aliases.htm

Note 1:  I decided to restrict the output columns to just the alias’s name and its definition.

Note 2:  ConvertTo-Html needs a file name; I used Out-File, but you could use the greater than arrow  > to redirect the results.

Note 3:  As I really wanted to view a table of cmdlets with their corresponding alias, I called for Invoke-Item to open the .htm file.

Note 4:  There is no need to employ the backtick (`) to word-wrap the instruction to Out-File; if you place a pipe (|) at the very end of the line PowerShell realises that the command continues on the next line.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2:  Research Processes and Their Handles

The key to this example is tracing the processes, see how ConvertTo-Html filters just then name and handles.  If you want to research other properties then run this command:
Get-Process | Get-Member.

# PowerShell ConvertTo-Html Cmdlet
Get-Process | ConvertTo-Html ProcessName, Handles | Out-File handles.htm
Invoke-Item Handles.htm

Note 5:  In this ConvertTo-Html example the -property parameter is assumed.

See also PowerShell's Invoke-WebRequest ยป

Example 3:  Turning on the Html Style

The purpose of this example is to use html formatting to produce grid lines and even a background color.

$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;}"
$a = $a + "Table{background-color:#DFFFFF;border-collapse: collapse;}"
$a = $a + "TH{border-width:1px;padding:0px;border-style:solid;border-color:black;}"
$a = $a + "TD{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "</style>"
Get-Process | ConvertTo-Html ProcessName, Handles -head $a | Out-File handles.htm
Invoke-Item Handles.htm

Note 6:  Observe how the -head parameter controls the formatting via the $a variable.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Research ConvertTo-Html Parameters

# Research ConvertTo-Html Parameters
Get-Help ConvertTo-Html -full

Note 7:  PowerShell’s own Get-Help cmdlet reveals useful switches or parameters, for instance you can modify ConvertTo-Html’s default table view with the parameter -as List.  While most of the parameters are cosmetic, the -properties performs a useful filtering function.

More Members of the ConvertTo Family

# Research More ConvertTo Verbs
Get-Command -Verb Convert*

Other useful cmdlets in this family include ConvertFrom-Csv.

See also honorary member Export-CliXml »

Summary of PowerShell’s ConvertTo-Html Cmdlet

Once you have tried ConvertTo-Html you will see if this cmdlet is just what you need, or if Out-File or Out-Grid would be better.  In my opinion if you choose ConvertTo-Html then mostly you will need to append the Invoke-Item parameter to display the actual web page.

If you like this page then please share it with your friends


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-File   • Out-GridView   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.