PowerTip: Add Key-Value Pair to Ordered Dictionary

Summary: Add a key-value pair to the first position in an ordered dictionary.

Hey, Scripting Guy! Question How can I use Windows PowerShell to add a key-value pair to the first position in an ordered dictionary?

Hey, Scripting Guy! AnswerUse the Insert method, which takes an index (starting with zero), a key, and a value.
          To add color = blue to the ordered dictionary in the $od variable:

PS C:> $od

Name       Value

—-       —–

shape      rectangle

distance   50

 

PS C:> $od.Insert(0, “color”, “blue”)

PS C:> $od

 

Name       Value

—-       —–

color      blue

shape      rectangle

distance   50