Create Text Reports with Format-Table

by May 1, 2009

Format-Table is a great cmdlet to output formatted data. Sometimes, you may just be interested in the raw table data. You can simply hide the column headers using -hideTableHeaders.

Let's say you need a report of all the different file types in a folder. Here is how to generate such a report in one line:

dir $env:windir |
Group-Object extension -noElement |
Where-Object { $_.Name -ne '' } |
Sort-Object Count -descending |
Format-Table -hideTableHeaders Count, `
{ "Files of type $( $_.Name) " }

Likewise, this is how you create a simple text report that lists all the vendors of the software you are running:

'Currently running:'Get-Process |
Group-Object Company -noElement |
Where-Object { $_.Name -ne '' } |
Sort-Object Count -descending |
Format-Table -hideTableHeaders `
{ "$($_.Count) Programs made by '$($_.Name)'." }

You can then easily convert this into a text file report:

Get-Process |
Group-Object Company -noElement |
Where-Object { $_.Name -ne '' } |
Sort-Object Count -descending |
Format-Table -hideTableHeaders `
{ "$($_.Count) Programs made by '$($_.Name)'." } |
Out-File $homereport.txt& "$homereport.txt"

You should be aware that Format-* cmdlets must always be the last elements in a pipeline because they convert the pipeline objects into console formatting objects. If you want to output information to a HTML file or forward it onto other commands, use Select-Object instead of Format-Table:

Get-Process |
Group-Object Company -noElement |
Where-Object { $_.Name -ne '' } |
Sort-Object Count -descending |
Select-Object { "$($_.Count) Programs made by '$($_.Name)'." } |
ConvertTo-Html |
Out-File $homereport.htm& "$homereport.htm"