Skip angular brackets while using ConvertTo-Html


I recently faced a problem while using ConvertTo-Html cmdlet in Powershell. I had written some fancy code to show my output in email with fonts and colors but this cmdlet converts the angular brackets ‘<’ to ‘&lt’ and ‘>’ to ‘&gt’ while processing input into html. There is no way to skip this in the command itself.

I used the regular html tags to add colors and line breaks, but to my surprise, ConvertTo-Html cmdlet converted all my fancy code to garbage and I received output with <br>, <font> etc printed in my email as plain text.

It took me sometime to figure that out and so here is my blog on how I solved that issue to ease out the life of other programmers.

I used HttpUtility.HtmlDecode Method after ConvertTo-Html cmdlet. This method converts back the &lt and &gt sign to angular brackets and is coverted back into a perfect html code. HttpUtility.HtmlDecode converts a string that has been HTML-encoded for HTTP transmission into a decoded string. More about this method can be read at this MSDN link

Without HttpUtility.HtmlDecode

Sample Code

function send_email($to, $subject, $body)
{
$to = "abc@company.com"
$smtpServer = "yourSmtpServer"
$from = "sender@company.com"

$emailBodyTemplate="..."
Send-MailMessage -To $to -Subject $subject -Body $emailBodyTemplate -SmtpServer $smtpServer -From $from -  BodyAsHtml
}

$colorsTable = New-Object System.Data.DataTable "Colors"
$colorsNameCol = New-Object System.Data.DataColumn ColorName,([string])
$colorsTable.columns.add($colorsNameCol)

$colorsTable.Rows.Add("Black Color<br>One more Black Color")
$colorsTable.Rows.Add("<font color='Red'>Red Color</font><br>")
$colorsTable.Rows.Add("<font color='Green'>Green Color</font><br>")
$colorsTable.Rows.Add("<b><font color='Yellow'>Bold Yellow Color</font></b>")

$body = $colorsTable | ConvertTo-Html -Property ColorName
Send_Email -body $body -subject "Daily Report"

Mail

Notice here the tags are coming as text in email

Converted HTML Code

Notice here the angular brackets have been converted into &lt and &gt.

With HttpUtility.HtmlDecode

Sample Code

function send_email($to, $subject, $body)
{
$to = "abc@company.com"
$smtpServer = "yourSmtpServer"
$from = "sender@company.com"

$emailBodyTemplate="..."
Send-MailMessage -To $to -Subject $subject -Body $emailBodyTemplate -SmtpServer $smtpServer -From $from -BodyAsHtml
}

Add-Type -AssemblyName System.Web

$colorsTable = New-Object System.Data.DataTable "Colors"
$colorsNameCol = New-Object System.Data.DataColumn ColorName,([string])
$colorsTable.columns.add($colorsNameCol)

$colorsTable.Rows.Add("Black Color<br>One more Black Color")
$colorsTable.Rows.Add("<font color='Red'>Red Color</font><br>")
$colorsTable.Rows.Add("<font color='Green'>Green Color</font><br>")
$colorsTable.Rows.Add("<b><font color='Yellow'>Bold Yellow Color</font></b>")

$body = $colorsTable | ConvertTo-Html -Property ColorName
$bodyDecode = [System.Web.HttpUtility]::HtmlDecode($body)
Send_Email -body $bodyDecode -subject "Daily Report"

Mail

Notice here the tags are converted into actual HTML code

Converted HTML

Notice here the angular brackets are retained in the code

Do let me know your feedback. Happy scripting!!!


Comments (0)

Skip to main content