Powershell 3.0 has a quick way to created hashtables that maintain their order as well at the cost of a slight memory expenditure.
The Problem:
# create ordered hashtable
$a = [ordered]@{}
# populate with 3 key-value pairs
$a['a'] = 15
$a['b'] = 10
$a['c'] = 9
$a
# Name Value
# ---- -----
# a 15
# b 10
# c 9
# get value for key = 'b'
$a['b']
# returns: 10
# get value for indexed last (ordered) item in table: This is desired behaviour
$a[-1]
# returns: 9
# show type of object for variable $a
$a | Get-Member
# returns: TypeName: System.Collections.Specialized.OrderedDictionary
# ...
All is well so far.
When serializing the object to disk, then deserializing, the object changes from System.Collections.Specialized.OrderedDictionary to Deserialized.System.Collections.Specialized.OrderedDictionary.
# serialize to disk
$a | Export-CliXml ./serialized.xml
# deserialize from disk
$d = Import-CliXml ./serialized.xml
# show datatype of deserialized object
# returns: Deserialized.System.Collections.Specialized.OrderedDictionary
# ...
# $d variable is still indexed by key, but not by integer index
$d['b']
# returns: 10
# request the first item in the dictionary by numerical index
$d[0]
# returns: nothing/null, this is undesired behaviour. AARGH!
Naturally, I believe the deserialized Ordered Dictionary should behave as the Ordered Dictionary did before it had been persisted to disk, specifically to be able to retrieve hashtable items by numerical index as well as key.
Since that is not currently the case in Powershell, is there a quick way to convert the deserialized object to the base version of the object without doing a foreach on the entire deserialized object?