PowerTip: Add Key-Value Pair to Ordered Dictionary

PowerTip: Add Key-Value Pair to Ordered Dictionary

  • Comments 1
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Nice One