How about turning a single object into a hash table? This way, you can display one object property per line when you output the object in a grid view window:

# get any object
$object = Get-Process -Id $pid

# try and access the PSObject
$hash = $object.PSObject.Properties.Where{$null -ne $_.Value}.Name | 
    Sort-Object |
    ForEach-Object { $hash = [Ordered]@{} } { $hash[$_] = $object.$_ } { $hash }

# output regularly
$object | Out-GridView -Title Regular

# output as a hash table, only non-empty properties, sorted
$hash | Out-GridView -Title Hash

Twitter This Tip! ReTweet this Tip!

  • Very nice, but with interesting side effects. I tried this today in WinPS 5.1:

    (get-aduser otter -Properties *){![string]::IsNullOrEmpty($_.value) -and $_.value.count} | Sort-Object Name | Select Name,Value

    The 'Name' column appeard left align in the console, as expected, but "Value" column turned out right aligned.

    I suspected some magically inserted whitespace to be the reason, so I added a trim...

    (get-aduser otter -Properties *){![string]::IsNullOrEmpty($_.value) -and $_.value.count} | Sort-Object Name | Select Name,@{N='Value';E={$_.Value.trim(' ')}}

    partial success: "Value" was left aligned again, but all integer values were blanked. Testing for value -is [string] did not help. So I replaced the trim with a -replace:

    (get-aduser otter -Properties *){![string]::IsNullOrEmpty($_.value) -and $_.value.count} | Sort-Object Name | Select Name,@{N='Value';E={$_.Value -replace '^ (?=\S)(?<=\S) $'}}

    This finally worked. Am I the only one who thinks, this is strange?

  • Thanks for the tip. i will use it next time I need it.