Generating alternate colors in an email HTML table generated by PowerShell in SharePoint

Generating rich text emails with PowerShell

In this post: Generating automatic emails with embedded-reports and link to uploaded csv
we explored sending emails and attachments with HTML tables. Now let’s have a look at alternating row colors in the HTML.

This function will be used later to inject CSS reference to alternating colors into the HTML Table:

Function Set-AlternatingRows {
Begin {
$ClassName = $CSSEvenClass
Process {
If ($Line.Contains("<tr>"))
{	$Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
If ($ClassName -eq $CSSEvenClass)
{	$ClassName = $CSSOddClass
{	$ClassName = $CSSEvenClass
Return $Line

Let’s now define $a as the styles, including the style for alternating rows, which we will use when generating the HTML table:

$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;}"
$a = $a + "Table{background-color:#EFFFFF;border-collapse: collapse;}"
$a = $a + "TH{border-width:1px;padding:5px;border-style:solid;border-color:black;background-color:#DDDDDD}"
$a = $a + "TD{border-width:1px;padding-left:5px;padding-right:3px;border-style:solid;border-color:black;}"
$a = $a + ".odd { background-color:#ccddee; }"
$a = $a + ".even { background-color:#eeeeff; }"
$a = $a + "</style>"

In generating the HTML table, note the reference to the above function in the Pipeline:

$MyOutput = $table | ConvertTo-Html Title, Link, Author, Size, FileName, Modified -head $a -body "<H1>$($rows.count) $($Header)</H1>"  | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | Convert-HTMLEscape

As a refresher, we use this function to handle HTML escaping so the HTML comes out delightfully formatted:

Function Convert-HTMLEscape {
convert &lt; and &gt; to < and > It is assumed that these will be in pairs
Param (
Process {
foreach ($item in $text) {
if ($item -match "&lt;") {
else {
#otherwise just write the line to the pipeline
} #close process
} #close function
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *