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.
Create HTML Reports using PowerShell
The challenge with .txt is most of the time it is hard to read and with .csv users must-have the office installed on their machine. To avoid this dependency today we will learn how to export output in an HTML file. Since it is HTML no dependency and easily readable.
4 good reasons learn this skill?
- PowerShell can communicate and harvest the data required from machine stacked in Microsoft platform.
- Using PowerShell and HTML reports can easily merge in user-friendly fashion
- Since HTML can run with any browser it doesn’t any other tool to be installed like MS-Office and Notepad ++
- 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
PowerShell ConvertTo-HTML parameter
This parameter converts the output into an 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 into an HTML format by using convert-html parameter and also I am selecting a selective column as part of the report.
If you execute this command it will generate HTML file with the selected columns as below:
If we see the HTML file code it will be as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/></colgroup>
<tr><th>ProcessName</th><th>CPU</th><th>VM</th></tr>
<tr><td>acnamagent</td><td></td><td>126590976</td></tr>
.
.
.
Etc
</table>
</body></html>
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.
Merge output of more than one PowerShell script
There some times we might end up generating a single report with the output from more than one script/line of commands.
Ex: if the system admin wants 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 the 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 here is PowerShell generate head and title tag for each execution and the 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 File will be as below with multiple head tags:
This is not the right approach.
Implement reports using –Fragment
The right approach is to generate the same output using –Fragment. Since we are using “-Fragment” PowerShell considers output from each line as a 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.
This way we can generate with many parameters.
Implement 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 inside PowerShell script
$Header = @"
<style>
table{
font-family:"Trebuchest MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
border: 2px solid;
border-color:black;
width: 100%;
}
th{
padding-top:12px;
padding-bottom:12px;
text-align:left;
background-color:#06AF8F;
color:black;
}
</style>
<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:
You may like following PowerShell SharePoint tutorials:
- PowerShell out gridview in SharePoint 2013/2016
- Delete Corrupted Service Applications in SharePoint 2013
- SharePoint 2013/2016 PowerShell Script Examples
- Get SharePoint Online Site and List Templates using PowerShell
- Get SharePoint 2013 Server Disk Space using PowerShell
- Get Content DataBase Size Using PowerShell in SharePoint 2013
- SharePoint 2013 Create Site Column and Content Types Using PowerShell
- Change list or library URL in SharePoint 2013/2016
- Create Site Collection and Subsite in SharePoint Online using PowerShell
- PowerShell to clean up user information in SharePoint 2013/2016
- PowerShell recursive function example
- Create List or Document Library using PowerShell in SharePoint Online
- Open html file in a browser in SharePoint 2013/2016
- Export user permission in SharePoint 2013/2016 using PowerShell
This PowerShell tutorial helps us to learn how to create HTML Reports using PowerShell.
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