In the previous tip we introduced ConvertObject-ToHashTable which makes it easy to dump objects in a grid view window.

The code below is an improved version that sorts properties alphabetically, and lets you decide how the columns should be called. By default, Out-GridView calls the columns “Property” and “Value”, but you can override the defaults and call them anything:

function ConvertObject-ToHashTable
{
    param
    (
        [Parameter(Mandatory,ValueFromPipeline)]
        $object,
 
        [string]
        $PropertyName = "Property",
 
        [string]
        $ValueName = "Value"
    )
 
    process
    {
    $object | 
        Get-Member -MemberType *Property | 
        Select-Object -ExpandProperty Name |
        Sort-Object |
        ForEach-Object { [PSCustomObject]@{ 
            $PropertyName = $_
            $ValueName = $object.$_} 
        }
    }
}
 
Get-ComputerInfo | 
  # by default, columns are named "Property" and "Value"
  ConvertObject-ToHashTable | 
  Out-GridView
 
 
Get-WmiObject -Class Win32_BIOS | 
  # specify how you'd like to call the columns displayed in a grid view window
  ConvertObject-ToHashTable -PropertyName Information -ValueName Data | 
  Out-GridView

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this year’s PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu

Twitter This Tip! ReTweet this Tip!

Anonymous