Powershell

How to Use ConvertTo-Html (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

PowerShell’s “ConvertTo-Html” cmdlet is used to convert .NET objects or output into an HTML web page. The converted web page can be viewed in a web browser. Various parameters can be used with the stated cmdlet to get customized output, such as to display the output in the table or list form. It is quite a useful tool when the user wants to PowerShell console output in the web browser.

This tutorial will guide the users to convert the output to an HTML web page.

How to Use ConvertTo-Html (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

To convert an object or output of a command into an HTML page. First, place any cmdlet and then pipe it to the “ConvertTo-Html” cmdlet using the pipe operator “|”. Doing so will output a large string in the PowerShell console.

Let’s explore some of the examples to know further about the stated cmdlet.

Example 1: Use the “ConvertTo-Html” Cmdlet to Convert an Object’s Output into an HTML Page
To convert an object’s or cmdlet’s output to an HTML web page, first, place the cmdlet. Then, pipe it to the “ConvertTo-Html” cmdlet using the pipe “|” operator:

Get-Service | ConvertTo-Html

Example 2: Convert a Commands Output into an HTML Page and Save it into a Specified File
To send output to a text file, pipe the command to the “Out-File” cmdlet. Then, use the “-FilePath” operator and assign it the target file path:

Get-Date | ConvertTo-Html | Out-File -FilePath C:\Docs\Dateinfo.html

Execute the following command to check if the output was saved into a file or not:

Get-Content C:\Docs\Dateinfo.html

Example 3: Create a Web Page Displaying the Services on it
First, mention the “ConvertTo-Html” cmdlet. Then, use the “-InputObject” parameter and assign it the cmdlet that needs to be converted into HTML enclosed within parentheses:

ConvertTo-Html -InputObject (Get-Service)

That’s it! You have learned the usage of the “ConvertTo-Html” cmdlet in PowerShell.

Conclusion

The “ConvertTo-Html” cmdlet is used to convert the PowerShell console’s output into an HTML web page. For this specified purpose, specify the cmdlet and then pipe it to the “ConvertTo-Html” cmdlet. This post has elaborated on PowerShell’s “ConvertTo-HTML” with the help of various examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.