Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

HTML Bits and Pieces, Part 3

Now that you're able to create HTML reports, how about doing something with it? Here's how to send it as an e-mail with the Send-MailMessage cmdlet.

In the last few lessons we've been exploring ways of creating HTML reports in PowerShell using ConvertTo-HTML. This time, lets' look at some other fun we can have with HTML and PowerShell. First, let's send an HTML document as an e-mail.

The Send-MailMessage cmdlet includes a switch parameter that indicates the body is HTML code. This makes it very easy to distribute HTML based reports. Even better, you don't even need a complete HTML document. You should be able to get by with fragments, although if you want to use a style sheet you need to embed it in the HTML fragment. For example, I'll start by defining my style tag which will be part of the message body:

<style type='text/css'>
font-family:Tahoma, Arial, Helvetica, sans-serif;
border:1px #0000FF solid;
padding:5px 5px 5px 5px;
name tr

Next, I'll add some data to it:

$body += get-process | where { $_.WS -ge 10MB} | Select ID,Name,WS,Path | ConvertTo-Html -Fragment -As List -PreContent "<h3>Process Workingset Size >= 10MB</h3>"

I'm creating a list fragment and also inserting a header caption before my data. All that remains is to e-mail the fragment to myself:

Send-MailMessage -to -from -subject "Top Processes" -body ($body | out-string) -BodyAsHtml -SmtpServer -Credential

There is one little trick here and that is to make sure the body is a string and not an array of string. That's why I pipe $body to Out-String. The end result is that in my inbox I get a formatted HTML report (see Fig. 1).

Sample HTML report as it might look in one's e-mail.

Figure 1. Sample HTML report as it might look in one's e-mail. (Click image to view larger version.)

Or perhaps your document could use a little navigation. Here's a simple example. First I'll define a string that includes the navigation links:

$nav = @"
<a href='#ComputerSystem'>ComputerSystem</a>
<a href='#OperatingSystem'>OperatingSystem</a>
<a href='#Drives'>Drives</a>
<a href='#' target='_top'>Top</a>

To use them I also need to define an anchor name for each section:

$data += Get-WmiObject win32_Computersystem | Select Manufacturer,Model,TotalPhysicalMemory | ConvertTo-Html -PreContent "<h2><a name='ComputerSystem'>ComputerSystem</a></h2>" -post $nav -Fragment

This command gets data from WMI and creates an HTML fragment. Before the data I'm displaying a heading that includes defining the anchor name and after the fragment are my navigation links. I'll repeat this for the other data in my report with a break between tables:

$data += Get-WmiObject win32_OperatingSystem | Select Caption,*Memory,
@{Name="LastBoot";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} |
ConvertTo-Html -PreContent "<h2><a name='OperatingSystem'>OperatingSystem</a></h2>" -post $nav -Fragment

$data += Get-WmiObject Win32_logicaldisk -filter "DriveType=3" |
Select DeviceID,@{Name="SizeMB";Expression={($_.Size/1MB) -as [int]}},
@{Name="FreeMB";Expression={[math]::Round(($_.Freespace/1MB),2)}} |
ConvertTo-Html -PreContent "<h2><a name='Drives'>Drives</a></h2>" -post $nav -Fragment

At this point, all that remains is to assemble the final document and save it to a file:

ConvertTo-Html -InputObject $null -Title "System Report" -PreContent "<H1>System Report</H1>" -PostContent $data -css \\jdh-nvnas\files\blue.css | Out-File c:\work\sysreport.htm

Same HTML report, now with navigation.

Figure 2. Same HTML report, now with navigation. (Click image to view larger version.)

There's really no limit to what you can accomplish. If you would like to experiment with this further, you can download a zip file with my sample CSS file and PowerShell code that I used to generate this report.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Thu, Nov 7, 2013 Retta family insurance healthcare DOT

Wed, Nov 6, 2013 Diandra car insurance quotes nc DOT

Sun, Sep 29, 2013 Caroline business insurance 0% apr credit cards DOT DOT

Fri, Sep 27, 2013 Peerless small business insurance cost estimate DOT

Sun, Sep 22, 2013 Xandy credit card 0% apr DOT

Sun, Sep 22, 2013 Maribeth cash back credit card DOT

Fri, Sep 20, 2013 Jenibelle real cialis cheap smart people budget car insurance DOT DOT

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above