#PSTip Use Excel to View HTML Output

Excel can be used to view HTML.  If you add some CSS styles to the result of ConvertTo-HTML you can add much more rich output to Excel then you would get with a simple Export-Csv

$HTMLFile = Join-Path $Home "Processes.html"
$HTML = Get-Process | Select-Object CPU, ID, ProcessName | ConvertTo-HTML

# Reference for color names http://www.w3schools.com/cssref/css_colornames.asp
$HTML = $HTML -replace '^[<]tr[>][<]td[>][<][/]td[>]','<tr style="color:red" ><td></td>'

# Highlight anything that has Chrome or Google In the Name
$HTML = $HTML -replace '[<]td(?<T>[>]((chrome)|(Google[^<]*))[<][/]td[>])','<td style="background:blue;color:Yellow" ${T}'
$HTML | Out-File $HTMLFile 

#Find a good version of Excel.exe
$Excel = Resolve-Path "C:\Program Files*\Microsoft Office\Office*\EXCEL.EXE" | 
            Select-Object -First 1 -ExpandProperty Path  

& $Excel $HTMLFile 

excelcolor

About the author: Josh Miller

Josh Miller is the Operations Lead for Trintech's Dataflow Services offering, this involves a mixture of time in Visual Studio, the PowerShell ISE, and on phone calls with clients. Josh is also a leader of the DFW PowerShell user group.

Related Posts

%d bloggers like this: