Powershell

How to Create An HTML Report With PowerShell

PowerShell is a scripting language known for its flexibility and its simplicity. It allows the conversion of PowerShell output to an HTML report. The HTML report can be created using the “ConvertTo-Html” cmdlet in PowerShell. The newly created HTML report will be exported to the desired location by using the “Out-File” cmdlet.

This article will overview the details to create an HTML report in PowerShell.

How to Create An HTML Report With PowerShell?

As stated above the HTML report can be created using the “ConvertTo-HTML” cmdlet. The examples explaining the concept clearly are described below.

Example 1: Create an HTML Report of Windows Services List

In this demonstration, the Windows services list will be converted to an HTML report and then saved into a file:

Get-Service | ConvertTo-HTML | Out-File C:\Doc\NewReport.html

 

According to the above code:

  • First, define the “Get-Service” cmdlet along with the pipeline “|” to transfer the output of the previous command to the next.
  • Then, specify the “ConvertTo-HTML” cmdlet, and again add the pipeline “|”.
  • After that, define the “Out-File” cmdlet and specify the target file path along with the “.html” extension:

Let’s verify whether the conversion was successful or not by running the code below:

Get-ChildItem C:\Doc\NewReport.html

 

It can be observed that the HTML report has been generated successfully.

Example 2: Create an HTML Report of Event Logs

This illustration will convert the PowerShell event log into an HTML report using the “ConvertTo-HTML” cmdlet:

Get-EventLog -LogName "Windows PowerShell" | ConvertTo-Html | Out-File C:\Doc\pslog.html

 

In the above code snippet:

  • First, add the “Get-EventLog” cmdlet to get the list of logs.
  • Then, write the “-LogName” parameter and assign the “Windows PowerShell” along with the pipeline “|”.
  • Then, add the “ConvertTo-Html” cmdlet along with pipeline “|” again.
  • Lastly, write the “Out-File” cmdlet and assign the target file path:

Let’s verify whether an HTML report was created:

Get-ChildItem C:\Doc\pslog.html

 

Example 3: Create an HTML Report of Event Log Having Specified Properties

This example will convert the event log with selective properties into an HTML report:

Get-EventLog -Log "Windows PowerShell" | ConvertTo-Html -Property id, task, level

 

In the above code snippet:

  • Firstly, write “Get-EventLog” cmdlet along with the “-Log” parameter and then assign the “Windows PowerShell” value.
  • Then, add the pipeline “|” along with the “ConvertTo-Html” cmdlet.
  • Lastly, define the “-Property” parameter and assign the properties separated by a comma:

As you can see that the HTML report has been generated successfully.

Conclusion

The HTML report in PowerShell can be created using the “ConvertTo-HTML” cmdlet. To do so, first, add the cmdlet or string to be converted into HTML. After that, place the pipeline “|” and then add the “ConvertTo-HTML” cmdlet. Then again, place the pipeline “|” and finally add the target path to save the HTML report. This tutorial has observed a complete guide about creating an HTML report.

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.