Email Array of Objects Using PowerShell

I use a couple different email functions to send different reports. This can be very useful to get script results back when you setup PowerShell based scheduled tasks.  This particular function adds an HTML header that formats the table a little nicer than the ConvertTo-HTML cmdlet does on its' own. It then sends out an HTML formatted email which. You just need to pass an array of objects to the function along with the normal email type information. (NOTE: using Select-Object to specify exactly what you want will help with the clarity of the email) Here is an example where I'm emailing myself the list of processes currently running on a system

Example Usage:

Untitled - PowerGUI Script Editor
$to = ""
$from = ""
$subject = "List of Running Processes on " + $env:COMPUTERNAME
$smtp = ""
$content = get-process | select processname,id
Send-EmailHTML -To $to -From $from -Subject $subject -SMTPServer $smtp -BodyAsArray $content

Resulting Email:


Function Code:


Untitled - PowerGUI Script Editor
function Send-EmailHTML {
Sends HTML Formatted Email
Send-EmailHTML will take an array of objects and emails a table in HTML 
$to = ""
$from = ""
$subject = "List of Running Processes on " + $env:COMPUTERNAME
$smtp = ""
$content = get-process | select processname,id
Send-EmailHTML -To $to -From $from -Subject $subject -SMTPServer $smtp -BodyAsArray $content

This will email a table of all running processes on the system returned from the Get-Process 
cmdlet.  Using a select statement returns the information with clarity
    [CmdletBinding()] param (
        [parameter(Mandatory=$true)] [string]$To,
        [parameter(Mandatory=$true)] [string]$From,
        [parameter(Mandatory=$true)] [string]$Subject,
        [parameter(Mandatory=$true)] [string]$SMTPServer,
        [parameter(Mandatory=$true)] [array]$BodyAsArray,
        [parameter(Mandatory=$false)] [string]$CC,
        [Parameter(Mandatory=$false)] [string]$BCC

        #html style definition
        $htmlHeader = @'
                body { background-color:#FFFFFF; } 
                body,table,td,th { font-family:Tahoma; color:Black; Font-Size:10pt } 
                th { font-weight:bold; background-color:#1F497D; color:White } 
                td { background-color:#DDDDDD; }
        try {
            #convert array into HTML fragment string
            $htmlContent = $BodyAsArray | ConvertTo-Html -Fragment | Out-String
            #initialize mail object
            $mail = new-object System.Net.Mail.MailMessage
            $mail.from = $From
            if ($CC) { 
            if ($BCC) { 
            $mail.subject = $Subject
            #add html to mail body
            $html = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString( ($htmlHeader + $htmlContent), $null, "text/html" )
            $mail.IsBodyHtml = 1
            #send mail
            $smtpClient = new-object System.Net.Mail.SmtpClient
            $smtpClient.Host = $SMTPServer
        catch {
            Write-Host "`n" $_.Exception.Message "`n" -ForegroundColor Magenta
Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>