$path="$env:temp\logshipping_rpt.htm"
#Define an Empty Array to Hold all of the HTML Fragments
$fragments = @()
#Define the HTML style
$head = @"
<style>
body { background-color:#FAFAFA;
font-family:Arial;
font-size:12pt; }
td, th { border:1px solid black;
border-collapse:collapse; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
tr:nth-child(odd) {background-color: lightgray}
table { margin-left:50px; }
</style>
<H2>This is a test report</H2>
"@
$results = invoke-sqlcmd -query "YOUR QUERY" -ServerInstance 'ServerName'
$fragments += "<br>This report is the output of the powershell script</i><br><br>"
$fragments += $results | ConvertTo-HTML -Fragment
ConvertTo-HTML -Head $head -Body $fragments -PostContent "<br><br><i>report generated: $(Get-Date)</i>" | Out-File -FilePath $path -Encoding ascii
#Define mail variables
$smtp = "SMTPSERVER"
$to = "DESTINATION_EMAIL"
$from = "SOURCE_EMAIL"
$subject = "This is a test Email"
#Format Body and send the email
$body = (get-content $path) | out-string
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml
This script should output the result into a html file as well as emailing the result.