Description

This Script will Send a PSObject or other Object from PowerShell in a table format, using a HTML email.

The main point of this script is that it applies HTML formatting using a Cascading Style Sheet (CSS).

Once this script has been configured it then becomes very easy to send email reports in the "Pretty table format".

Just call the cmdlet with the mandatory parameters: InputObject, To & Subject.

Usage:

$FileInfo = Get-ChildItem -Filter *.xls | Select-Object Name, Length

Send-HTMLEmail -InputObject $FileInfo -To User@domain.org -Subject "Directory Information"

Results:

You will receive the following table in the body of the email.

 

I updated this script to now include the CSS embedded in a here-string, or else you can send in the link to the file.

I previously used the CSS shown below, however you do not need that anymore.

You can download and save the CSS file and update the script with the location if you want to customize the CSS format rather than use the default embedded CSS.

http://gallery.technet.microsoft.com/scriptcenter/c63025ef-645d-4efe-a4c3-bc126cb939e8

 

Script

PowerShell
Edit|Remove
<# 
.SYNOPSIS 
Send an email with an object in a pretty table 
.DESCRIPTION 
Send email 
.PARAMETER InputObject 
Any PSOBJECT or other Table 
.PARAMETER Subject 
The Subject of the email 
.PARAMETER To 
The To field is who receives the email 
.PARAMETER From 
The From address of the email 
.PARAMETER CSS 
This is the Cascading Style Sheet that will be used to Style the table 
.PARAMETER SmtpServer 
The SMTP relay server 
.EXAMPLE 
PS C:\> Send-HtmlEmail -InputObject (Get-process *vmware* | select CPU, WS) -Subject "This is a process report" 
An example to send some process information to email recipient 
.NOTES 
NAME        :  Send-HtmlEmail 
VERSION     :  1.1.0    
LAST UPDATED:  01/03/2013 
AUTHOR      :  Milo 
.INPUTS 
None 
.OUTPUTS 
None 
#>  
 
function Send-HTMLEmail { 
#Requires -Version 2.0 
[CmdletBinding()] 
 Param  
   ([Parameter(Mandatory=$True, 
               Position = 0, 
               ValueFromPipeline=$true, 
               ValueFromPipelineByPropertyName=$true, 
               HelpMessage="Please enter the Inputobject")] 
    $InputObject, 
    [Parameter(Mandatory=$True, 
               Position = 1, 
               ValueFromPipeline=$true, 
               ValueFromPipelineByPropertyName=$true, 
               HelpMessage="Please enter the Subject")] 
    [String]$Subject,     
    [Parameter(Mandatory=$False, 
               Position = 2, 
               HelpMessage="Please enter the To address")]     
    [String[]]$To = "user@domain.com", 
    [String]$From = "Admin@domain.com",     
    [String]$CSS, 
    [String]$SmtpServer ="smtprelay.domain.com" 
   )#End Param 
 
if (!$CSS) 
{ 
    $CSS = @" 
Please download the code from the link rather than copy from here, since the code does not show up here. 
"@ 
}#End if 
 
$HTMLDetails = @{ 
    Title = $Subject 
    Head = $CSS 
    } 
     
$Splat = @{ 
    To         =$To 
    Body       ="$($InputObject | ConvertTo-Html @HTMLDetails)" 
    Subject    =$Subject 
    SmtpServer =$SmtpServer 
    From       =$From 
    BodyAsHtml =$True 
    } 
    Send-MailMessage @Splat 
     
}#Send-HTMLEmail