{"id":2742,"date":"2013-10-11T00:01:00","date_gmt":"2013-10-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/11\/convertto-ordereddictionary\/"},"modified":"2023-05-16T13:10:30","modified_gmt":"2023-05-16T20:10:30","slug":"convertto-ordereddictionary","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/convertto-ordereddictionary\/","title":{"rendered":"ConvertTo-OrderedDictionary"},"content":{"rendered":"<div>\n<p><strong>Summary<\/strong>: Learn about creating\u00a0ordered dictionaries in\u00a0Windows PowerShell\u00a03.0.<\/p>\n<p><a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135175\" target=\"_blank\" rel=\"noopener\">Hash tables<\/a> are fabulous for storing data items that are associated with each other. The simple &#8220;key=value&#8221; format is easy to create and easy to search.<\/p>\n<p>The only problem is that the order of elements in the hash table is arbitrary. If you need to have the items in a particular order, you need to sort them every time.<\/p>\n<pre><code>\r\nPS C:\\&gt; $hash = @{a=1; b=2; c=3}\r\nPS C:\\&gt; $hash\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\n\r\nPS C:\\&gt; $hash.GetEnumerator() | sort\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n<\/code><\/pre>\n<p>A new feature in Windows PowerShell\u00a03.0 makes it easy to create ordered dictionaries, which are like hash tables that maintain the order of elements. To create an ordered dictionary, simply place the <strong>[ordered]<\/strong> attribute before the &#8220;@&#8221; symbol.<\/p>\n<pre><code>\r\nPS C:\\&gt; $dictionary = [ordered]@{a=1; b=2; c=3}\r\nPS C:\\&gt; $dictionary\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nb\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n<\/code><\/pre>\n<p>Be sure to place the <strong>[ordered]<\/strong> before the <strong>@<\/strong> and not before the variable name. If you mess up, you get the &#8220;The ordered attribute can be specified only on a hash literal node&#8221; error message, which is very accurate, I&#8217;m sure, but isn&#8217;t easy to interpret without a computer science degree. Interpret it as, &#8220;You can&#8217;t put the [ordered] attribute on a variable. Put it right before the @.&#8221;<\/p>\n<pre><code>\r\nPS C:\\&gt; [ordered]$dictionary = @{a=1;b=2;c=3}\r\n\r\nAt line:1 char:1\r\n+ [ordered]$dictionary = @{a=1;b=2;c=3}\r\n+ ~~~~~~~~~~~~~~~~~~~~\r\nThe ordered attribute can be specified only on a hash literal node.\r\n\u00a0\u00a0\u00a0 + CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ParserError: (:) [], ParentContainsErrorRecordException\r\n\u00a0\u00a0\u00a0 + FullyQualifiedErrorId : OrderedAttributeOnlyOnHashLiteralNode\r\n<\/code><\/pre>\n<p>And the <strong>[ordered]<\/strong> attribute works ONLY on hash tables. You can&#8217;t use it on other types of collections, such as arrays (comma-separated lists), because they don&#8217;t have the key-value pair format.<\/p>\n<pre><code>\r\nPS C:\\&gt; $colors = [ordered](\"red\", \"green\", \"blue\")\r\nAt line:1 char:11\r\n+ $colors = [ordered](\"red\", \"green\", \"blue\")\r\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\nThe ordered attribute can be specified only on a hash literal node.\r\n\u00a0\u00a0\u00a0 + CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ParserError: (:) [], ParentContainsErrorRecordException\r\n\u00a0\u00a0\u00a0 + FullyQualifiedErrorId : OrderedAttributeOnlyOnHashLiteralNode\r\n<\/code><\/pre>\n<p>Ordered dictionaries are instances of the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.collections.specialized.ordereddictionary\" target=\"_blank\" rel=\"noopener\">System.Collections.Specialized.OrderedDictionary<\/a> class. In addition to many hash table-like properties and methods, you get a few goodies that are available because of the preserved order, such as indexing. You can index into items in an ordered dictionary. The following command gets the value of the item at index 1. (Remember that the index starts at zero.)<\/p>\n<pre><code>\r\nPS C:\\&gt; $dictionary\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n\r\nPS C:&gt; $dictionary[1]\r\n2\r\n<\/code><\/pre>\n<p>You can insert key-value pairs at a particular index position. The syntax of the <strong>Insert<\/strong> method is:\u00a0<code>Insert(&lt;index&gt;, &lt;key&gt;, &lt;value&gt;)<\/code><\/p>\n<pre><code>PS C:\\&gt; $dictionary.Insert(1, \"d\", 2.5)\r\nPS C:\\&gt; $dictionary\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.5\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n<\/code><\/pre>\n<p>You can remove key-value pairs at a particular index position. The syntax of the <strong>RemoveAt<\/strong> method is: <code>RemoveAt(&lt;index&gt;)<\/code><\/p>\n<pre><code>\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.5\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n\r\nPS C:\\&gt; $dictionary.RemoveAt(2)\r\nPS C:\\&gt; $dictionary\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a02.5\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n<\/code><\/pre>\n<p>To perform operations on each element in an ordered dictionary, you can use a <strong>ForEach<\/strong> loop. That&#8217;s what I usually do.<\/p>\n<pre><code>\r\nPS C:\\&gt; foreach ($value in $dictionary.values){$sum += $value}\r\nPS C:\\&gt; $sum\r\n6.5\r\n<\/code><\/pre>\n<p>But because it&#8217;s indexed, you can also get values by using a traditional <strong>For<\/strong> loop.<\/p>\n<pre><code>\r\nPS C:\\&gt; for ($i = 0; $i -lt $dictionary.count; $i++)\r\n&gt;&gt; {$sum += $dictionary[$i]}\r\n\r\nPS C:\\&gt; $sum\r\n6.5\r\n<\/code><\/pre>\n<p>All terrific stuff.\u00a0 But if you forget about ordered dictionaries and create a standard hash table, you can&#8217;t use the <strong>[ordered]<\/strong> attribute to cast or convert a hash table to an ordered dictionary. If you do&#8230;<\/p>\n<pre><code>PS C:\\&gt; $newDict = [ordered]$hash\r\nAt line:1 char:12\r\n+ $newDict = [ordered]$hash\r\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ~~~~~~~~~~~~~~\r\nThe ordered attribute can be specified only on a hash literal node.\r\n\u00a0\u00a0\u00a0 + CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ParserError: (:) [], ParentContainsErrorRecordException\r\n\u00a0\u00a0\u00a0 + FullyQualifiedErrorId : OrderedAttributeOnlyOnHashLiteralNode\r\n<\/code><\/pre>\n<p>So, I wrote a little script that converts a hash table to an ordered dictionary. Okay, it doesn&#8217;t really convert anything. It creates a new ordered dictionary, sorts the keys in the hash table alphanumerically, adds the sorted key-value pairs in the hash table to the ordered dictionary, and returns the ordered dictionary.\nHere&#8217;s how you use it:<\/p>\n<pre><code>\r\nPS C:\\&gt;$hash = @{a=1;b=2;c=3}\r\n#Oops! I meant to create a dictionary\r\nPS C:\\&gt;$hash = .ConvertTo-OrderedDictionary.ps1 -Hash $hash\r\nPS C:\\&gt; $hash | Get-Member\r\n\r\n\u00a0\u00a0 TypeName: System.Collections.Specialized.OrderedDictionary\r\n\r\nPS C:\\&gt; $hash\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n\r\nPS C:\\&gt; $hash.Insert(1, \"ItWorks!\", [System.Math]::pi)\r\nPS C:\\&gt; $hash\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\na\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\r\nItWorks!\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3.14159265358979\r\nb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\r\nc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\r\n<\/code><\/pre>\n<p>Ed mentioned that people also need to convert arrays to ordered dictionaries, so I added a little feature that does the deed. The keys in the ordered dictionary are integers beginning with 0, like this:<\/p>\n<pre><code>\r\n$winter = \"December\", \"January\", \"February\"\r\n$winter = .ConvertTo-OrderedDictionary.ps1 -Hash $winter\r\n\r\nPS C:\\&gt; $winter\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\n0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 December\r\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 January\r\n2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 February\r\n<\/code><\/pre>\n<p>If you convert the script to a function, you can pipe hash tables to it. The following command uses the <strong>AsHashTable<\/strong> parameter of the <strong>Group-Object<\/strong> cmdlet to get a hash table of the <strong>Convert<\/strong> cmdlets in each module. The command passes the hash table to <strong>ConvertTo-OrderedDictionary<\/strong>, which returns an ordered dictionary.<\/p>\n<pre><code>\r\nPS C:\\&gt; $d = Get-Command Convert* | Group-Object -Property ModuleName -AsHashTable | ConvertTo-OrderedDictionary\r\nPS C:\\&gt; $d\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {ConvertTo-OrderedDictionary, convert.exe}\r\nMicrosoft.PowerShell.Manage... {Convert-Path}\r\nMicrosoft.PowerShell.Security\u00a0 {ConvertFrom-SecureString, ConvertTo-SecureString}\r\nMicrosoft.PowerShell.Utility\u00a0\u00a0 {ConvertFrom-Csv, ConvertFrom-Json, ConvertFrom-StringData, ConvertTo-Csv...}\r\nMSOnline\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {Convert-MsolDomainToFederated, Convert-MsolDomainToStandard, Convert-MsolFederatedUser}\r\nMSOnlineExtended\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0{Convert-MsolFederatedUser}\r\nTrustedPlatformModule\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {ConvertTo-TpmOwnerAuth}\r\n\r\nPS C:&gt; $d.gettype().fullname\r\nSystem.Collections.Specialized.OrderedDictionary\r\n<\/code><\/pre>\n<p>The script is included in the <a href=\"https:\/\/www.powershellgallery.com\/packages\/PoshFunctions\/2.2.9\">PoshFunctions<\/a> module in the PowerShell Gallery. You can find the source at <a href=\"https:\/\/github.com\/riedyw\/PoshFunctions\/blob\/master\/Functions\/ConvertTo-OrderedDictionary.ps1\" target=\"_blank\" rel=\"noopener\">ConvertTo-OrderedDictionary<\/a>. Have fun!\n~June\nThanks, June!<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn about creating\u00a0ordered dictionaries in\u00a0Windows PowerShell\u00a03.0. Hash tables are fabulous for storing data items that are associated with each other. The simple &#8220;key=value&#8221; format is easy to create and easy to search. The only problem is that the order of elements in the hash table is arbitrary. If you need to have the items [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,56,370,3,4,45],"class_list":["post-2742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-guest-blogger","tag-june-blender","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn about creating\u00a0ordered dictionaries in\u00a0Windows PowerShell\u00a03.0. Hash tables are fabulous for storing data items that are associated with each other. The simple &#8220;key=value&#8221; format is easy to create and easy to search. The only problem is that the order of elements in the hash table is arbitrary. If you need to have the items [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2742"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2742\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}