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

One of my co-workers, 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′},
             @{n=’Type’;e={$PSItem.Description};width=5} -AutoSize

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!

 

TechEd NZ: Windows Server 2012 PowerShell Crash Course

I was fortunate enough to be asked to present at TechEd NZ this year. I had a standing-room only room (Yay!), and a really good response from the crowd.

All the sessions that were recorded at TechEd NZ 2012 are now available.

And here’s my session:

Windows Server 2012 PowerShell Crash Course

Enjoy!