hash tables in powershell

Question

I'm developing an application in PowerShell. I am storing variables in a hashtable. How can I keep the order in the hashtable? I want the order to be the same as I when I filled the hashtable.

Solution

Hash tables by nature don't maintain the order of values. There are a few workarounds already out on the net. Check these

http://www.tellingmachine.com/post/2009/01/When-PowerShell-hash-table-magic-backfires.aspx

http://huddledmasses.org/powershell-and-hashtable-oddities/

Or Try

PS C:\WINDOWS\system32> $OrderedList = New-Object System.Collections.Specialized.OrderedDictionary
PS C:\WINDOWS\system32> $OrderedList
PS C:\WINDOWS\system32> $OrderedList.Add("Name","Ravi")
PS C:\WINDOWS\system32> $OrderedList.Add("Age","30")
PS C:\WINDOWS\system32> $OrderedList

Name                           Value
----                           -----
Name                           Ravi
Age                            30