Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Some Style

Posted on August 31, 2010August 30, 2010

Windows PowerShell has many ways to present and store information. You can display it to the screen, write it to a file, send it to a printer, create an CSV or XML file or even a pretty HTML report. The ConvertTo-HTML cmdlet underwent a significant facelift for v2.0 and is a popular cmdlet. But I don't think admins know how to fully take advantage of the cmdlet or have the right tools.
For many admins, they might use ConvertTo-HTML in an expression like this.
[cc lang="PowerShell"]
Get-WMIObject -class win32_logicaldisk -filter "drivetype=3" -computername "Serenity","Quark","jdhit-dc01" |
Select -Property SystemName,DeviceID,VolumeName,Size,FreeSpace |
ConvertTo-HTML -Title "Drive Report" | out-file c:\work\basicinfo.html
[/cc]
This expression uses Get-WMIObject to connect to 3 computers, return some information about fixed disks, and create an HTML report. Remember, ConvertTo-HTML only does the conversion. You still need to send it to Out-File to save the results. I'll end up with a simple table like this. Very basic. Plain. Boring.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Fortunately ConvertTo-HTML offers a number of parameters for creating some very nice looking reports. The cmdlet essentially takes input and creates a simple table. But you can insert arbitrary HTML code before the table using -PreContent as well as after the table with -PostContent. Use -Title to define an title (what else?). Real style though is done with a CSS file. Specify the CSS file with the -CSSUri parameter. I've included a sample css file you can download and use to get started.

Here's a more complete version of my basic example. Notice how I take advantage of the ConvertTo-HTML parameters.
[cc lang="PowerShell"]
Get-WMIObject -class win32_logicaldisk -filter "drivetype=3" -computername "Serenity","Quark","jdhit-dc01" |
Select -Property SystemName,DeviceID,VolumeName,@{Name="SizeGB";Expression={ "{0:N4}" -f ($_.Size/1GB)}},`
@{Name="FreeGB";Expression={ "{0:N4}" -f ($_.FreeSpace/1GB)}} |
ConvertTo-HTML -Title "Drive Report" `
-PreContent "

graphic/Fixed Disk Report

" `
-CssUri ".\sample.css" -PostContent " report run $(get-date) by $env:userdomain\$env:username"|
out-file c:\work\fancyinfo.html
[/cc]
The CSS file is expected to be found in the same directory as the HTML file. Curious as to what I ended up with? Click here. It has been my experience that using CSS files works better when viewing the file in Internet Explorer. I'm not an HTML developer by any means so it's possible Firefox doesn't like the CSS files I use, but I get better results when viewing the final HTML in IE.

Feel free to use my sample css file and tweak as necessary. If you have a CSS file that creates very nice PowerShell reports, I hope you'll consider sharing.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

4 thoughts on “Get Some Style”

  1. jv says:
    August 31, 2010 at 10:52 am

    For Firefox try using a dufferent DOCTYPE (4.0?) and be sure to use CSS 2.0 versions of all styles. The biggest differences between FF and IE are in DIV and TABLE tag behaviors. Ther eare numeraous liitle CSS tricks that will force both browsers to behave.
    We are told that IE9 fixes all of this. HTML 5 and CSS3 are both very powerful and will, hopefully, end all the hoopla. From what I have seem I think MS has gotten it this time.

    Very good write up. I will keep it for reference on ConvertTo-HTMJ to hand out to others.

    1. Jeffery Hicks says:
      August 31, 2010 at 11:33 am

      Thanks for that explanation. My experience with CSS file is tweaking something someone else already created. I usually tell students to work with their web developers to get style sheets with corporate-approved formatting.

      1. jv says:
        September 1, 2010 at 12:06 pm

        That is good advice. We can also put CSS style sheets on a network drive and use them.

        ALso note that we can point at one style sheet and then add changes in a second or in a local ‘style’ block that can selectively override the master corporate style sheet.
        Learning a bit about ‘style’ is a good thing if you do many HTML reports. The plus is that with PowerShell, as always, there are many ways to accomlish a task but most have someti8ng to do with ‘Standards’. Css is a standard that will continue to show up everywhere. XAML will even become slave to CSS as will all Windows Forms. It’s worth learning.

  2. jv says:
    August 31, 2010 at 11:06 am

    It just hit me that this might be useful and fun to play with.

    To perform advanced controlled edits just use XML.

    The DOCTYPE of teh output html is the following:
    !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”
    Notice that it is “XHTML strict” which makes it pure XML and can be edited with teh XMLDOM.

    [xml]$xml =get-process|convertto-html

    Note: To get to the html you will need to select into the declared namespace.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d