{"id":13421,"date":"2011-07-05T00:01:00","date_gmt":"2011-07-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/07\/05\/automatically-create-a-powershell-hash-table\/"},"modified":"2011-07-05T00:01:00","modified_gmt":"2011-07-05T00:01:00","slug":"automatically-create-a-powershell-hash-table","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/automatically-create-a-powershell-hash-table\/","title":{"rendered":"Automatically Create a PowerShell Hash Table"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to automatically create a Windows PowerShell hash table and extra process data.<\/p>\n<p class=\"CodeBlock\">&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I thought <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/learn-the-basics-of-powershell-hash-tables\/\">your article introducing hash tables<\/a> was pretty interesting. I was wondering how I might populate a hash table. Can you explain how to do this easily?<\/p>\n<p>&mdash;AC<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>&nbsp;Hello AC,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. This is a pretty cool time of the year down here in Charlotte, North Carolina. Well, not literally cool, but figuratively anyway. The thing that is so nice is that fresh peaches are showing up everywhere. This is because locally grown peaches from South Carolina and from Georgia are easily obtained.<\/p>\n<p>AC, if I need to populate a hash table with two or three key\/value pairs, I generally do it manually and separate each key\/value pair with a semicolon. One thing to note is that while the value<i> <\/i>portion requires quotation marks, the key portion does not. In the last key\/value pair, I illustrate this. The code to create three key\/value pairs, assign them to a hash table stored in the variable <i>$hash<\/i>, and then display the contents of that variable is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash = @{&#8220;key1&#8243;=&#8221;value1&#8243;;&#8221;key2&#8243;=&#8221;value2&#8243;;key3=&#8221;value3&#8221;}<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">key3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value3<\/p>\n<p style=\"padding-left: 30px\">key2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value2<\/p>\n<p style=\"padding-left: 30px\">key1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value1<\/p>\n<p>&nbsp;<\/p>\n<p>If I want to retrieve a specific key, I use the <b>item<\/b> method as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.Item(&#8220;key2&#8221;)<\/p>\n<p style=\"padding-left: 30px\">value2<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.Item(&#8220;key3&#8221;)<\/p>\n<p style=\"padding-left: 30px\">value3<\/p>\n<p>One of the things I like to do is to create an empty hash table and assign it to a variable. I will do this whether I am working in the Windows PowerShell console or using the Windows PowerShell ISE. To create an empty hash table, I use the @ sign followed by an opening and a closing brace. The three characters are assigned to a variable. This technique appears here:<\/p>\n<p style=\"padding-left: 30px\">$hash = @{}<\/p>\n<p>An empty hash table object is created and is stored in the variable. This is verified by piping the object stored in the variable to the <b>Get-Member<\/b> cmdlet. The technique to retrieve the members appears here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash | Get-Member&nbsp;<\/p>\n<p style=\"padding-left: 30px\">TypeName: System.Collections.Hashtable&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void Add(System.Object key, System.Object value)<\/p>\n<p style=\"padding-left: 30px\">Clear&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void Clear()<\/p>\n<p style=\"padding-left: 30px\">Clone&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object Clone()<\/p>\n<p style=\"padding-left: 30px\">Contains&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool Contains(System.Object key)<\/p>\n<p style=\"padding-left: 30px\">ContainsKey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ContainsKey(System.Object key)<\/p>\n<p style=\"padding-left: 30px\">ContainsValue&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ContainsValue(System.Object value)<\/p>\n<p style=\"padding-left: 30px\">CopyTo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void CopyTo(array array, int arrayIndex)<\/p>\n<p style=\"padding-left: 30px\">Equals&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool Equals(System.Object obj)<\/p>\n<p style=\"padding-left: 30px\">GetEnumerator&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Collections.IDictionaryEnumerator GetEnumerator()<\/p>\n<p style=\"padding-left: 30px\">GetHashCode&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int GetHashCode()<\/p>\n<p style=\"padding-left: 30px\">GetObjectData&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo inf&#8230;<\/p>\n<p style=\"padding-left: 30px\">GetType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type GetType()<\/p>\n<p style=\"padding-left: 30px\">OnDeserialization&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void OnDeserialization(System.Object sender)<\/p>\n<p style=\"padding-left: 30px\">Remove &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void Remove(System.Object key)<\/p>\n<p style=\"padding-left: 30px\">ToString&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string ToString()<\/p>\n<p style=\"padding-left: 30px\">Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ParameterizedProperty &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object Item(System.Object key) {get;set;}<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Int32 Count {get;}<\/p>\n<p style=\"padding-left: 30px\">IsFixedSize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean IsFixedSize {get;}<\/p>\n<p style=\"padding-left: 30px\">IsReadOnly&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean IsReadOnly {get;}<\/p>\n<p style=\"padding-left: 30px\">IsSynchronized&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean IsSynchronized {get;}<\/p>\n<p style=\"padding-left: 30px\">Keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Collections.ICollection Keys {get;}<\/p>\n<p style=\"padding-left: 30px\">SyncRoot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object SyncRoot {get;}<\/p>\n<p style=\"padding-left: 30px\">Values&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Collections.ICollection Values {get;}<\/p>\n<p>After I have an empty hash table, I use the <b>add<\/b><i> <\/i>method to populate the hash table with key\/value pairs. The first thing I do is create an array of 100 integers by using the <b>range<\/b> operator. Next, I use the percentage sign (which is the alias for the <b>ForEach-Object<\/b> cmdlet) and call the <b>add<\/b> method. As previously discussed, the <b>key<\/b> property does not require quotation marks, but the <b>value<\/b> property does require them. The code to add 100 integers as key\/value pairs to a hash table is shown in the following code:<\/p>\n<p style=\"padding-left: 30px\">1..100 | % { $hash.Add($_,&#8221;$_&#8221;) }<\/p>\n<p style=\"padding-left: 30px\">To empty a hash table and permit reuse of the hash table, use the <b>clear<\/b><i> <\/i>method. To verify that the hash table is empty, use the <b>count<\/b> property. The technique of clearing the hash table of data and verifying that it is empty is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.Clear()<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.Count<\/p>\n<p style=\"padding-left: 30px\">0<\/p>\n<p>Now that I have an empty hash table, I am going to populate it with the process ID and the process name of every running process on my system. Because it is likely that there could be more than one process with the same name (<i>svchost<\/i>, for example), I use the process ID (PID) as the <b>key<\/b> property and the name of the process as the associated value. The process information is easily obtained by using the <b>Get-Process<\/b> cmdlet.<\/p>\n<p style=\"padding-left: 30px\">Get-Process | % { $hash.Add($_.id,$_.name) }<\/p>\n<p>When working with a hash table, I often need to look at only the keys. To get a listing of only the keys, use the <b>keys<\/b> property. This is illustrated in the following code where the <b>keys<\/b> property is used retrieve all of the keys and the <b>Select-Object<\/b> cmdlet (<b>select<\/b> is an alias for the <b>Select-Object<\/b> cmdlet) is used to limit the results to the first four keys:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.Keys | select -First 4<\/p>\n<p style=\"padding-left: 30px\">2620<\/p>\n<p style=\"padding-left: 30px\">1652<\/p>\n<p style=\"padding-left: 30px\">1412<\/p>\n<p style=\"padding-left: 30px\">4040<\/p>\n<p>In the same manner, the values of a hash table can be obtained by using the <b>values<\/b> property. As you will remember, the <b>value<\/b> property of a hash table does not need to be unique. In the example of processes running on my computer, I know this is a case.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.count<\/p>\n<p style=\"padding-left: 30px\">64<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; ($hash.Values | select -Unique | Measure-Object).count<\/p>\n<p style=\"padding-left: 30px\">50<\/p>\n<p>The 64 running processes appear in Task Manager. As shown in the following figure, there are a number of duplicate processes.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1643.hsg-7-5-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of duplicate processes\" alt=\"Image of duplicate processes\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1643.hsg-7-5-11-01.png\" \/><\/a><\/p>\n<p>The <b>values<\/b> property of the hash table returns all of the values that are associated with the key\/value pairs. The <b>count<\/b><i> <\/i>property tells me how many key\/value pairs exist. By taking the collection of values and piping the results to the <b>Select-Object<\/b> cmdlet, I can use the <b>unique<\/b> switch to obtain only the unique items. I can then send the resulting collection of unique values to the <b>Measure-Object<\/b> cmdlet and choose the <b>count<\/b> property. The result tells me how many unique processes are running. The code and associated output to do this are shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; $hash.count<\/p>\n<p style=\"padding-left: 30px\">64<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.WOODGROVE&gt; ($hash.Values | select -Unique | Measure-Object).count<\/p>\n<p style=\"padding-left: 30px\">50<\/p>\n<p>I might like to see which processes have duplicates. In addition, I might like to see how many instances of each process are running. A reasonable list might include the top ten instances of these processes. To do this, I use the <b>values<\/b><i> <\/i>property to return a list of all the values stored in the hash table, and I group them by value. Next, I sort them by the value of the <b>count<\/b> property, and choose the first 10 instances. (In the following command <strong>group<\/strong>, <b>sort<\/b> and <b>select<\/b> are aliases for <b>Group-Object<\/b>, <b>Sort-Object<\/b>, and <b>Select-Object<\/b> respectively). The command and associated output are shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C &gt; $hash.values | group -NoElement | sort count -Descending | select -First 10<\/p>\n<p style=\"padding-left: 30px\">Count &nbsp;&nbsp; Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8211; &nbsp;&nbsp;&nbsp;&nbsp; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 11 &nbsp;&nbsp;&nbsp;&nbsp; svchost<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 3 &nbsp;&nbsp;&nbsp;&nbsp; iexplore<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 2 &nbsp;&nbsp;&nbsp;&nbsp; csrss<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 2 &nbsp;&nbsp;&nbsp;&nbsp; conhost<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 1 &nbsp;&nbsp;&nbsp;&nbsp; IAStorDataMgrSvc<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 1 &nbsp;&nbsp;&nbsp;&nbsp; SearchFilterHost<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 1 &nbsp;&nbsp;&nbsp;&nbsp; sqlwriter<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 1 &nbsp;&nbsp;&nbsp;&nbsp; SnagPriv<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 1 &nbsp;&nbsp;&nbsp;&nbsp; dwm<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 1 &nbsp;&nbsp;&nbsp;&nbsp; explorer<\/p>\n<p>AC, that is all there is to using hash tables. Hash Table Week will continue tomorrow when I will talk about using hash tables to filter lists.<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to automatically create a Windows PowerShell hash table and extra process data. &nbsp; Hey, Scripting Guy! I thought your article introducing hash tables was pretty interesting. I was wondering how I might populate a hash table. Can you explain how to do this easily? &mdash;AC &nbsp; &nbsp;Hello AC, Microsoft Scripting Guy Ed [&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":[18,51,3,4,45],"class_list":["post-13421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to automatically create a Windows PowerShell hash table and extra process data. &nbsp; Hey, Scripting Guy! I thought your article introducing hash tables was pretty interesting. I was wondering how I might populate a hash table. Can you explain how to do this easily? &mdash;AC &nbsp; &nbsp;Hello AC, Microsoft Scripting Guy Ed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13421","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=13421"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13421\/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=13421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}