PowerShell script to send an email with SQL results in HTML format Does anyone have a PowerShell script to query results from a SQL Server database and send it out as an email in HTML format?
Following is a script I use to send query results in an HTML-formatted email.  Basically pipe the result of invoke-sqlcmd to ConvertTo-Html and then to Out-String, and then massage it a bit to get the table to display nicely.  If you don't add the padding style, everything will run together.

Send-MailMessage example below is for unathenticated, so if you have to authenticate to your SMTP server you'll need to fix that up

$Alerts = invoke-sqlcmd -ServerInstance dbserver -Database dbname -Query "SELECT * FROM some_table" | SELECT Severity, ServerName, Heading | ConvertTo-Html | Out-String
$Alerts = $Alerts.Replace("&lt;/head&gt;", "&lt;style&gt; TD {padding-left: 2mm}&lt;/style&gt;&lt;/head&gt;")
Send-MailMessage -To me@mycompany.com -From me@mycompany.com -BodyAsHtml $Alerts -Subject "Alerts" -SmtpServer "smtp.mycompany.com"