Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ConvertTo-HtmlTabel - Make Pretty HTML formatted tables
#Requires -Version 3.0
Function ConvertTo-HtmlTable{
Converts table objects into HTML text for web page conversion and/or emailing as html reports.
Will convert any object array into a formatted HTML string output for saving as a web page or for emailing.
Doesn't handle nested objects, please only provide a two-demensional table as input.
Input object(s) to be converted to a formatted HTML table
.PARAMETER BorderColor
Table border color in simple names, default is "black".
.PARAMETER BackgroundColor
Table background color in hex RGB, default is "DFDFDF".
Text font color in hex RGB, default is "000000" (black)
.PARAMETER BoarderWidth
Table boarder width in pixels, default is 1.
.PARAMETER PaddingWidth
Table boarder padding width in pixels, the distance the boarder is from the text, default is 5.
Get-Process | ConvertTo-HtmlTable | Set-Content .\process_report.html
Makes an HTML file from the current process list.
Send-MailMessage -Body (ConvertTo-HtmlTable -Table (Get-Process)) -BodyAsHtml -To $to -From $from -Subject "Test" -smtpServer "exchangerelay1"
Sends an email message with a nicely formatted table in the body of the email.
Author: Bryan Vine
Last updated: 6/24/2015
[string]$BorderColor = "black",
[string]$BackgroundColor = "DFDFDF",
[string]$FontColor = "000000",
[int]$BorderWidth = 1,
[int]$PaddingWidth = 5
$AllTabels = @()
$textoutput = "<font color=#$FontColor>"
$head = "<style>"
$head += "TABLE{border-width: $($BorderWidth)px;border-style: solid;border-color:$BorderColor;}"
$head += "Table{background-color:#$BackgroundColor;border-collapse: collapse;}"
$head += "TH{border-width:$($BorderWidth)px;padding:$($PaddingWidth)px;border-style:solid;border-color:$BorderColor;}"
$head += "TD{border-width:$($BorderWidth)px;padding:$($PaddingWidth)px;border-style:solid;border-color:$BorderColor;}"
$head += "</style>"
$AllTabels += $table
$textoutput += $AllTabels | ConvertTo-Html -Head $head
$textoutput += "</font>"
Write-Output $textoutput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.