Hash to Arrayof Objects

As an old-time Perl user, I love hashes.  In Perl, hashes are the answer, no matter what the question is.  (No, not really, but they solve a lot of problems.)  In PowerShell, hashes are cool, but you can’t do things like “Sort-Object -property value”.

Here’s a quick-and-dirty way to convert a hash into an array of objects:

$list = $hash.Keys |  Select-Object -Property @{ n = “name”; e = { $_; } }, @{ n = “value”; e = { $hash.$_; } }

Note that this is a one-way function.  All keys in a hash are guaranteed unique.  However, an array of name/value pairs are  not guaranteed to have unique names.

Also note that this works best for names with simple datatypes, such as [String] or [int].

Comments (0)