If you are doing to be viewing this mail in the Outlook client you should know that CSS support is very limited. Like Cobster and other comments are suggesting nth:child css style is the way to go for this in a general sense but that does no good where Outlook is concerned. It will not render td:nth-child
. Also it is not a good practice to insert a complete html document as a body for an email. It does work however. You should be using -Fragment
but you lose your $style
. If you can accept those caveats this will work fine (minus the alignment in Outlook). It would display fine in a modern browser.
$head = @"
<style>
body{background-color:LightYellow;}
table{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
th{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}
td{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:palegoldenrod}
td:nth-child(1){text-align: left}
td:nth-child(2){text-align: center}
td:nth-child(3){text-align: right}
</style>
"@
# Output for -Body must be a single string.
$html = Get-ChildItem c:\temp | Select-Object name,lastwritetime,length | ConvertTo-Html -Head $head | Out-String
Send-MailMessage -From abc@xyz.com -to fgh@cat.com -Subject "USAGE REPORT" -BodyAsHtml $body -SmtpServer $smtpserver
I use output from Get-ChildItem
here for the "report".
Still though you do want this to open in Outlook so I would like to continue with this. It is a relatively dirty solution as most would cower at the approach. This is only meant to work in Outlook mail client. I am only doing it this way to show you how you can get the alignment you are looking for. I am sure there is room for improvement here. Basically we build the <table>
ourselves and add it to the SMTP mail.
# Start the body with the open tag for table and its style.
$body = @("<table cellspacing=1; cellpadding=5>")
# Define the style used for the table header
$headerStyle = 'bgcolor="d8bfd8"'
# Using an array store the unique attributes for the 1st,2nd and 3rd columns
$columnStyles = 'align="left"', 'align="center"', 'align="right"'
# Style shared/used by every element in each row.
$rowDataStyle = 'bgcolor="#EEE8AA"'
# Build the header row using the template. Since there is repetiion the format operator cleans up the code nicely.
$header = "<tr><th {0}>Name</th><th {0}>LastWriteTime</th><th {0}>length</th></tr>" -f $headerStyle
# Template used to build each row using the input object.
$htmlRowTemplate = "<tr><td {0};{1}>{2}</td><td {0};{3}>{4}</td><td {0};{5}>{6}</td></tr>"
# Build the table rows
$tableData = Get-ChildItem "c:\temp" | Select-Object name,lastwritetime,length | ForEach-Object{
# Use the format operator and the template to create the row in HTML
$htmlRowTemplate -f $rowDataStyle,
$columnStyles[0], $_.Name,
$columnStyles[1], $_.lastwritetime,
$columnStyles[2], $_.length
}
#Build the body. Which is just an html table.
$body = $body + $header + $tableData + "</table>" | Out-String
Send-MailMessage -From abc@xyz.com -to fgh@cat.com -Subject "USAGE REPORT" -BodyAsHtml $body -SmtpServer $smtpserver
To try and save on space I made large use of the -f
Format operator here which you can read up on. Here is the mail I generated from the above code in my Outlook 2010 Client (using my own smtp settings of course):

You will notice that your style is not perfectly replicated. You should be able to make your own changes as you need.