{"id":12391,"date":"2011-10-15T00:01:00","date_gmt":"2011-10-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/15\/easily-create-a-powershell-hash-table\/"},"modified":"2023-07-05T11:04:33","modified_gmt":"2023-07-05T18:04:33","slug":"easily-create-a-powershell-hash-table","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/easily-create-a-powershell-hash-table\/","title":{"rendered":"Easily Create a PowerShell Hash Table"},"content":{"rendered":"<p><strong><span style=\"font-size: 10pt;\">Summary:<\/span><\/strong><span style=\"font-size: 10pt;\"> Learn how to automatically populate a hash table in a Windows PowerShell script.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt 38.9pt;\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 9.5pt;\">\u00a0<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">Microsoft Scripting Guy Ed Wilson here. A hash table is an important data structure in Windows PowerShell. Many of the cmdlets use hash tables to format their input. For example, if I want to create a custom column header in a table, I have to use a hash table. A hash table consists of one or more key value pairs (of course, it is possible to create an empty hash table that contains no key value pairs, but let\u2019s go with the easy description first).<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">The at\u00a0sign\u00a0and a pair of braces (curly brackets) identify a hash table. Normally a variable stores the hash table, but it is possible to create a hash table and not store it in a variable. An example of this is shown here:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">@{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">&#8220;key1&#8221; = &#8220;value1&#8221;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">&#8220;key2&#8221; = &#8220;value2&#8221;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">In the following figure, I first run the code and display the contents of the hash table. Next, I pipe the results to the <strong>Get-Member<\/strong> cmdlet. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7357.hsg-10-15-11-1_6C9ACB5B.png\"><img decoding=\"async\" style=\"padding-left: 0px; padding-right: 0px; padding-top: 0px; border: 0px;\" title=\"Image of contents of hash table\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5488.hsg-10-15-11-1_thumb_4ED9409C.png\" alt=\"Image of contents of hash table\" width=\"504\" height=\"489\" border=\"0\" \/><\/a><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">Most of the time, a hash table is stored in a variable for use in other places. It is possible to create a hash table on a single line, but it is difficult to read, and if a problem occurs, it is hard to troubleshoot. The semicolon separates key value pairs and indicates a new line. In the code seen here, I create a hash table on a single line. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$hash = @{&#8220;key1&#8221; = &#8220;value1&#8243;;&#8221;key2&#8221; = &#8220;value2&#8221;}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">The same hash table is easier to read when spread out on multiple lines. This technique is shown here:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$hash1 = @{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\"\"><span style=\"font-size: 10pt;\">\u00a0 <\/span><span style=\"font-size: 10pt;\">&#8220;key1&#8221; = &#8220;value1&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\"\"><span style=\"font-size: 10pt;\">\u00a0 <\/span><span style=\"font-size: 10pt;\">&#8220;key2&#8221; = &#8220;value2&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\"\"><span style=\"font-size: 10pt;\">\u00a0<\/span><span style=\"font-size: 10pt;\">}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">Whether the closing brace appears on its own line or after the final key value pair is a matter of stylistic taste. I generally prefer to place it on line because it is easier to spot when troubleshooting. When working with a script editor that automatically matches brace pairs, this advantage disappears, and I then prefer to close up the code. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">The real power of hash tables comes by adding key value pairs automatically from within the script. When used in this way, hash tables are essentially temporary data storage. The advantage a hash table has over an array is the key value pairs. The keys provide a way to retrieve the associated value by name; with an array, the value is accessible via the element number. A disadvantage over an array is that with a hash table, the key must be unique; an array permits multiple elements to be the same. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">To create a hash table dynamically, follow these steps:<\/span><\/span><\/p>\n<p class=\"MsoListParagraphCxSpFirst\" style=\"line-height: 13.25pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in;\"><span style=\"\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">1.<\/span><\/span><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span><\/span><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">Create an empty hash table.<\/span><\/span><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"line-height: 13.25pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in;\"><span style=\"\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">2.<\/span><\/span><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span><\/span><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">Store the empty hash table in a variable.<\/span><\/span><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"line-height: 13.25pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in;\"><span style=\"\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">3.<\/span><\/span><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span><\/span><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">Collect the data.<\/span><\/span><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"line-height: 13.25pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in;\"><span style=\"\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">4.<\/span><\/span><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span><\/span><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">Store the collected data in a variable.<\/span><\/span><\/span><\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"line-height: 13.25pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in;\"><span style=\"\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">5.<\/span><\/span><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span><\/span><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">Use the <em>foreach<\/em> statement to walk through the collected data.<\/span><\/span><\/span><\/p>\n<p class=\"MsoListParagraphCxSpLast\" style=\"line-height: 13.25pt; list-style-type: disc; text-indent: -0.25in; margin: 0in 0in 8pt 0.5in;\"><span style=\"\"><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">6.<\/span><\/span><span style=\"line-height: normal;\"><span style=\"font-family: Times New Roman;\"><span style=\"font-size: 7pt;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span><\/span><span style=\"font-family: Segoe;\"><span style=\"font-size: 10pt;\">Inside the loop call the <strong>add<\/strong> method to add the key value pairs to the hash table.<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">An example of this procedure is shown here: <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$hash = $null<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$hash = @{}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$proc = get-process | Sort-Object -Property name -Unique<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">\u00a0<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">foreach ($p in $proc)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\"\"><span style=\"font-size: 10pt;\">\u00a0<\/span><span style=\"font-size: 10pt;\">$hash.add($p.name,$p.id)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">The first thing I do is assign the value <strong>$null<\/strong> to the <strong>$hash<\/strong> variable. I do this because when running code multiple times in the Windows PowerShell ISE, the values of global variables continue to be present. If I am not paying attention, the value stored in variables can change with each run of the script. After I have initialized the <strong>$hash<\/strong> variable with <strong>$null<\/strong>, I create an empty hash table and store it in the <strong>$hash<\/strong> variable. These two lines of code are shown here:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$hash = $null<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$hash = @{}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">Next I use the <strong>Get-Process<\/strong> cmdlet to collect information about each process that is running on the computer. I sort these process objects based upon the <strong>name<\/strong> property, and I use the <em>unique <\/em>switched parameter to return only unique instances of the process objects. The reason for doing this is I want to use the <strong>name<\/strong> property as the value for the keys in my hash table. The key of a hash table must be unique, and in most cases, there are several duplicate instances of processes running on a computer. For example, the following code reveals there are several processes named <strong>svchost<\/strong>. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\">\n<p>PS\nC:\\Users\\edwils&gt; gps | ? {$_.name -eq &#8216;svchost&#8217;}<\/p>\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\">\n<p><span style=\"text-decoration: underline;\">Handles<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"text-decoration: underline;\">NPM(K)<\/span> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"text-decoration: underline;\">PM(K)<\/span>\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"text-decoration: underline;\">WS(K)<\/span>\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"text-decoration: underline;\">VM(M)<\/span>\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"text-decoration: underline;\">CPU(s)<\/span>\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"text-decoration: underline;\">Id ProcessName<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\">\n<p>\u00a0\u00a0\u00a0 702\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10324\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 13464\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 56\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 612\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>443\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 15\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 6328\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 11808\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 53\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 936\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>165\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5512\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10580\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 48\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1036\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>653\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 28\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 28756\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 28172\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 93\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1120\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>789\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 31\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 16132\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 26284\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 119\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1152\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>2625\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 127\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 97160\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 88144\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 438\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1184\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>798\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 37\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 19064\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 22760\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 154\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1292\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>339\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 34\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 16332\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 19100\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 92\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1312\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>599\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 29\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 9136\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 16152\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 68\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1348\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>615\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 28\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 11352\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 15524\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 59\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1388\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>105\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 9\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2900\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 6644\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 35\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2052\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>86\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2596\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5660\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 47\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2332\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>350\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 15\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7444\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 9340\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 47\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4256\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>524\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 22\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8832\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 11304\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 56\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4812\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p>50\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1520\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3336\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 13\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5560\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 svchost<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\">\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">When I use the <em>unique <\/em>switched parameter, I retrieve only one instance of each process with the same name. I do not know which instance I obtain, but for this application, it does not matter. This line of code is shown here: <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">$proc = get-process | Sort-Object -Property name -Unique<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">Now it is time to walk through the collection of process objects and add the name and path to the hash table. I use the name of the process for the key and the process ID as the value. The <strong>foreach<\/strong> statement is thebest command to use to walk through the collection of process objects. I use the <strong>add<\/strong> method from the <strong>hashtable<\/strong> object that is stored in the <strong>$hash<\/strong> variable. The <strong>add<\/strong> method requires both the key and the value. This portion of the code is shown here: <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">foreach ($p in $proc)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\"\"><span style=\"font-size: 10pt;\">\u00a0<\/span><span style=\"font-size: 10pt;\">$hash.add($p.name,$p.id)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt 0.5in; line-height: 13.25pt; padding-left: 30px; list-style-type: disc;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">When I display the contents of the <strong>$hash<\/strong> variable, I am presented with the following output.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-10-15-11-2_2DE5CDF5.png\"><img decoding=\"async\" style=\"padding-left: 0px; padding-right: 0px; padding-top: 0px; border: 0px;\" title=\"Image of contents of $hash variable\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4621.hsg-10-15-11-2_thumb_0CF25B4E.png\" alt=\"Image of contents of $hash variable\" width=\"504\" height=\"489\" border=\"0\" \/><\/a><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 9.5pt;\">\u00a0<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">That is all there is to dynamically creating a hash table. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"\"><span style=\"font-family: Segoe; font-size: 10pt;\">I invite you to follow me on <\/span><\/span><span style=\"font-size: 10pt;\"><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #0000ff; font-family: Segoe;\"><span style=\"text-decoration: underline;\">Twitter<\/span><\/span><\/a><span style=\" font-family: Segoe;\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"color: #0000ff; font-family: Segoe;\"><span style=\"text-decoration: underline;\">Facebook<\/span><\/span><\/a><span style=\" font-family: Segoe;\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #0000ff; font-family: Segoe;\"><span style=\"text-decoration: underline;\">scripter@microsoft.com<\/span><\/span><\/a><span style=\" font-family: Segoe;\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #0000ff; font-family: Segoe;\"><span style=\"text-decoration: underline;\">Official Scripting Guys Forum<\/span><\/span><\/a><\/span><span style=\" font-family: Segoe; font-size: 10pt;\">. See you tomorrow. Until then, peace.<\/span><\/p>\n<p style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\">\n<p style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><strong><span style=\"font-family: Segoe UI;\"><span style=\" font-size: 10pt;\">Ed Wilson, Microsoft Scripting Guy<\/span><\/span><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\">\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt; list-style-type: disc; margin: 0in 0in 8pt;\"><span style=\"font-family: Segoe;\"><span style=\" font-size: 10pt;\">\u00a0<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to automatically populate a hash table in a Windows PowerShell script. \u00a0 Microsoft Scripting Guy Ed Wilson here. A hash table is an important data structure in Windows PowerShell. Many of the cmdlets use hash tables to format their input. For example, if I want to create a custom column header in [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[292,3,4,45],"class_list":["post-12391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hashtables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to automatically populate a hash table in a Windows PowerShell script. \u00a0 Microsoft Scripting Guy Ed Wilson here. A hash table is an important data structure in Windows PowerShell. Many of the cmdlets use hash tables to format their input. For example, if I want to create a custom column header in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12391","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=12391"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12391\/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=12391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}