Create HTML Reports using PowerShell

html report using powershell tutorial

This PowerShell tutorial explains, how to create an HTML report using PowerShell in SharePoint.

Today I would like to add an extra muscle to your PowerShell scripting. We all know that we use PowerShell scripting to automate manual work to improve productivity. When we generate reports using PowerShell most of the time the output is either .txt or .csv files.

SharePoint deveopment training course

The challenge with .txt is most of the time it is hard to read and with .csv user must have office installed on their machine. To avoid this dependency today we will learn how to export output in a HTML file. Since it is HTML no dependency and easily readable.

4 good reasons learn this skill?
1. PowerShell can communicate and harvest the data required from machine stacked in Microsoft platform.
2. Using PowerShell and HTML reports can be easily emerged in user friendly fashion
3. Since HTML can run with any browser it doesn’t any other tool to be installed like MS-Office and Notepad ++
4. HTML page runs in Web gives better user interaction and handy

What are we learning in this article?
– Using ConvertTo-HTML parameter
– How to merge output of more than one PowerShell script
– How to implement reports using –Fragment
– Implementing CSS in HTML reporting

Using ConvertTo-HTML parameter:
This parameter converts the output in to a HTML and it can be rendered in any browser seamlessly.
Get-Process | ConvertTo-Html -Property ProcessName,CPU, VM | Out-File -FilePath Process.html

Get-Process, gets the processes that are running on the local computer or a remote computer. Now I am converting the output in to a html format by using convert-html parameter and also I am selecting selective column as part of the report.

If you execute this command it will generates html file with the selected columns as below:

html report using powershell
html report using powershell

If we see the html file code it will be as follows:
<html xmlns=””>
<title>HTML TABLE</title>

It is including head, title, body and table properties by default, if you observe the title is random and default value.
We can change the Title of this HTML explicitly as below:

Get-Process | ConvertTo-Html -Property ProcessName,CPU, VM -Title “Process Details” | Out-File -FilePath Process.html

In the above line I am explicitly specifying Title.

How to merge output of more than one PowerShell script:
There some time we might end up generating single report with the output from more than one script / line of commands.

Ex: if system admin want to generate a report of all the various services running on the server it is preferred to combine the output as one report.

This can be achieved by using parameter “–append” at the end of each execution, what exactly it does is this will append the output to the same file like as string concatenation. The challenge hear is PowerShell generate head and title tag for each execution and result set will be badly formatted.

Get-WmiObject -Class Win32_OperatingSystem | ConvertTo-Html | Out-File process.html
Get-WmiObject -Class Win32_BIOS | ConvertTo-Html | Out-File process.html -Append
Get-WmiObject -Class Win32_Service | ConvertTo-Html | Out-File process.html -Append

html report using powershell tutorial
html report using powershell tutorial

HTML File will be as below with multiple head tags:

powershell html report template
powershell html report template

This is not the right approach.

How to implement reports using –Fragment:
The right approach is generate the same output using –Fragment. Since we are using “-Fragment” PowerShell consider output from each line as portion of HTML body than creating a new head and body tags. The script will be is as follows:

$Os = Get-WmiObject -Class Win32_OperatingSystem | ConvertTo-Html -Fragment
$Bios = Get-WmiObject -Class Win32_BIOS | ConvertTo-Html -Fragment
$Services = Get-WmiObject -Class Win32_Service | ConvertTo-Html –Fragment
ConvertTo-Html -Body “$Os $Bios $Services” -Title “Fragmented HTML Report” | Out-File FragmentedHTMLReport.html

The output is as below with one head and title node.

creating html reports in windows powershell
creating html reports in windows powershell

This way we can generate with many parameters.

Implementing CSS in HTML reporting:

Now that we have explored how to export a single session output and multi session output to html, now let us focus on formatting the HTML output. To implement this we use CSS (Cascading Style Sheets).
Note: The CSS code we need to implement the formatting can be within the power shell script or we can create the CSS outside the script refer in any other script as required.
CSS in side PowerShell script:
$Header = @”
font-family:”Trebuchest MS”, Arial, Helvetica, sans-serif;
border-collapse: collapse;
border: 2px solid;
width: 100%;
<title>Fragmented HTML Report</title>

$Os = Get-WmiObject -Class Win32_OperatingSystem | ConvertTo-Html -Fragment
$Bios = Get-WmiObject -Class Win32_BIOS | ConvertTo-Html -Fragment
$Services = Get-WmiObject -Class Win32_Service | ConvertTo-Html –Fragment
ConvertTo-Html -Body “$Os $Bios $Services” -Head $Header | Out-File FragmentedHTMLReport.html

The output file with CSS formatting:

creating html reports in powershell
creating html reports in powershell

You may like following SharePoint PowerShell Tutorials:

Enjoy your reporting with PowerShell !!!

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)


About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →