How to Generate HTML from Command Output

Introduction

PowerShell has powerful feature to generate reports. One of them is to generate HTML from command output. This guide will walk you through the process of generating HTML from command output in PowerShell.

Solution

To generate HTML report from command output, you can follow these steps:

  1. Using ConvertTo-HTML cmdlet to convert command output to HTML
  2. Using Set-Content to write the HTML to a file

Using ConvertTo-HTML cmdlet

Before generating HTML document, we need to convert the command output to HTML using ConvertTo-Html. The following example will generate html based on the output from Get-Process.


Get-Process | ConvertTo-Html -Property Name, Id, WorkingSet

It will output html similar to below image:

ConvertTo-Html Output

Using Set-Content cmdlet

In order to generate HTML, the result from ConvertTo-HTML above must be piped to Set-Content command.


Get-Process | ConvertTo-Html -Property Name, Id, WorkingSet | Set-Content report.html

The result will look like below image:

Generated HTML

By default, the html will be displayed as Table based on -As parameter of ConvertTo-Html. This command also allows us to display HTML as List according to the help.


PS C:\powershell scripts> Get-Help ConvertTo-Html

NAME
    ConvertTo-Html

SYNTAX
    ConvertTo-Html [[-Property] <Object[]>] [[-Head] <string[]>] [[-Title] <string>] [[-Body] <string[]>]
    [-InputObject <psobject>] [-As {Table | List}] [-CssUri <uri>] [-PostContent <string[]>] [-PreContent <string[]>]
    [-Meta <hashtable>] [-Charset <string>] [-Transitional] [<CommonParameters>]

    ConvertTo-Html [[-Property] <Object[]>] [-InputObject <psobject>] [-As {Table | List}] [-Fragment] [-PostContent
    <string[]>] [-PreContent <string[]>] [<CommonParameters>]


ALIASES
    None


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help ConvertTo-Html -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=2096595.


Thus to display HTML as a list, we can use following command:


Get-Process | ConvertTo-Html -Property Name, Id, WorkingSet -As List | Set-Content report.html

The result will be like below image:

HTML content as list

Conclusion

In order to convert and generate HTML document, you need to combine ConvertTo-HTML with Set-Content command.