Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell ConvertTo-Html

PowerShell ConvertTo-Html

When testing PowerShell's ConvertTo-Html cmdlet, the best way of making sense of the output is if you employ Invoke-Item to view the resulting web page.

Topics for PowerShell's ConvertTo-Html

 ♣

Introduction to ConvertTo-Html

ConvertTo-Html feeds on input from another PowerShell cmdlet; the output is displayed in a web page.  Your alternatives are to save the data in a text file, in which case you would employ Out-File, do nothing and display the data in the PowerShell ISE, or call for ConvertTo-Html.

Example 1: List Aliases in a Html file

The purpose of this script is to simply list PowerShell's aliases in a web page.

# PowerShell ConvertTo-Html Example
Clear-Host
Get-Alias | ConvertTo-Html -property Name, Definition | `
Out-File aliases.htm
Invoke-Item aliases.htm

Note 1:  I decided to restrict the output columns to just the alias's name and its definition.

Note 2:  ConvertTo-Html needs a file name; I used Out-File, but you could use the greater than arrow  > to redirect the results.

Note 3:  As I really wanted to view a table of cmdlets with their corresponding alias, I called for Invoke-Item to open the .htm file.

Note 4:  This example employs the tiny backtick (`) to word-wrap the instruction to Out-File.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Example 2:  Research Processes and Their Handles

The key to this example is tracing the processes, see how ConvertTo-Html filters just then name and handles.  If you want to research other properties then run this command:
Get-Process | Get-Member.

# PowerShell ConvertTo-Html Example
Clear-Host
Get-Process | ConvertTo-Html ProcessName, Handles | Out-File handles.htm
Invoke-Item Handles.htm

Note 5:  In this ConvertTo-Html example the -property parameter is assumed.

Example 3:  Turning on the Html Style

The purpose of this example is to use html formatting to produce grid lines and even a background color.

Clear-Host
$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;}"
$a = $a + "Table{background-color:#DFFFFF;border-collapse: collapse;}"
$a = $a + "TH{border-width:1px;padding:0px;border-style:solid;border-color:black;}"
$a = $a + "TD{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "</style>"
Get-Process | ConvertTo-Html ProcessName, Handles -head $a | Out-File handles.htm
Invoke-Item Handles.htm

Note 6:  Observe how the -head parameter controls the formatting via the $a variable.

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Research ConvertTo-Html Parameters

# Research ConvertTo-Html Parameters
Clear-Host
Get-Help ConvertTo-Html -full

Note 7:  PowerShell's own Get-Help cmdlet reveals useful switches or parameters, for instance you can modify ConvertTo-Html's default table view with the parameter -as List.  While most of the parameters are cosmetic, the -properties performs a useful filtering function.

More Members of the ConvertTo Family

# Research More ConvertTo Verbs
Clear-Host
Get-Command -verb ConvertTo

Other useful cmdlets in this family include ConvertTo-Csv.

Summary of PowerShell's ConvertTo-Html

Once you have tried ConvertTo-Html you will see if this cmdlet is just what you need, or if Out-File or Out-Grid would be better.  In my opinion if you choose ConvertTo-Html then mostly you will need to append the Invoke-Item parameter to display the actual web page.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home  • Out-File  • Out-GridView  • -f format  • Files  • Pipe to file

Export-CSV  • Import-CSV  • Format-Table  • Format-List  • Read-Host  • Write-Host

ConvertTo-Html   • Windows PowerShell   • PowerShell 3.0 Redirection

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.