Auldhouse Blog

Learn How to Create Custom Column Heads for PowerShell Out-GridView

21-Nov-2014 12:07:20 / by James Finley

One of my co-workers at Auldhouse Computer Training, who shall remain nameless (but it rhymes with Jalcolm Meffrey), asked a good question today-namely “How do I take this custom formatting from a “Format-Table” command and use it in the gridview control (Out-GridView). It took me a couple of minutes, but the internet bestowed her bounties upon me once again. And, no I’m not talking about going to the hoochie-mama sites.

I found this old posting from Ed Wilson, “The Scripting Guy”. Thank you Ed!

Learn How to Create Custom Column Heads for PowerShell Out-GridView – Hey, Scripting Guy! Blog – Site Home – TechNet Blogs.


For those of you who won’t/can’t be bothered to read it, basically it boiled down to “Don’t use format-table, use select-object, and build your columns using the -f operator syntax”.

With some minor modifications, you turn this:

Get-CimInstance -ClassName Win32_LogicalDisk  |
Where-Object DriveType -eq 3 |
Format-Table @{n=’DriveLetter’;e={$PSItem.DeviceID}},
             @{n=’FreeSpace(GB)’;e={$PSItem.FreeSpace / 1GB};formatString=’N2′},
             @{n=’Size(GB)’;e={$PSItem.Size / 1GB};formatString=’N2′},

Into this:

Get-CimInstance -ClassName Win32_LogicalDisk  |
Where-Object DriveType -eq 3 |
Select-Object -Property @{Label=’DriveLetter’;Expression={$PSItem.DeviceID}},
    @{Label=’FreeSpace(GB)’;Expression={“{0:N2}” -f($PSItem.FreeSpace / 1GB)}},
    @{Label=’Size(GB)’;Expression={“{0:N2}” -f ($PSItem.Size / 1GB)}},
    @{Label=’Type’;Expression={$PSItem.Description}} |  Sort-object ‘FreeSpace(GB)’ | Out-Gridview

If you were so inclined, you could swap out “Out-Gridview” for “Format-Table” or whichever output command you needed.

Thanks Jalcolm (if that is your real name) for giving me something to dig into and solve!

BTW - next time you book on one of our MS Technical courses - back your learning by taking the exam.


Auldhouse Microsoft Exam Special Offers

Topics: Powershell

James Finley

Written by James Finley

I’m a Microsoft Certified Trainer (MCT), based in Auckland, New Zealand. I’m a Senior Systems Trainer for Auldhouse, the largest corporate computer training company in New Zealand. I spend most of my time training on Microsoft Exchange, Lync, SharePoint, PowerShell, Hyper-V and VMWare with a little Windows Server thrown in the mix just for fun. This blog is meant to give some guidance and insight into how to use and manage these products, as well as helping keep people up to date on topics of interest around Microsoft certifications. With a little random silliness thrown in from time to time. In my IT career I’ve been fortunate enough to have opportunities to contribute to books, review courseware, deliver sessions on behalf of Microsoft and speak at Microsoft TechEd and other events.