Object Magic (Part 4)

by Oct 29, 2019

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!