{"id":1237,"date":"2013-04-09T09:00:14","date_gmt":"2013-04-09T13:00:14","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=1237"},"modified":"2013-04-08T09:07:36","modified_gmt":"2013-04-08T13:07:36","slug":"hashtables-introduction","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/hashtables-introduction\/","title":{"rendered":"Hashtables: Introduction"},"content":{"rendered":"<p>I was recently working on a script for a co-worker who is trying to learn more powershell. In the script I had used a couple of hashtables to store information&#8230;which caused a couple of questions as he was reviewing the code. This post is an introduction to powershell hashtables.<\/p>\n<p>A hashtable stores indexed key-value pairs in memory and is similar to the vbscript dictionary object. To help describe these concepts, I have listed a few examples based on <a title=\"HTML Color Codes\" href=\"http:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2013\/04\/ColorCodes.csv\" target=\"_blank\">a CSV file containing HTML color codes<\/a>.<\/p>\n<p>The examples below will:<br \/>\n1.) Import the CSV into a powershell array object,<br \/>\n2.) Show how to use the array object to return a specific code given a color name<br \/>\n3.) Convert the object from step 1 into a hashtable<br \/>\n4.) Show how to use the hashtable to return a specific code given a color name<\/p>\n<pre><code class=\"language-1\">\r\n# Example 1:\r\n$colorCodes = Import-Csv ColorCodes.csv\r\n\r\n# Example 2:\r\n# Using just the array object created from Import-CSV\r\n# we can select the color code for Red with just one line\r\n($colorCodes | Where-Object {$_.ColorName -eq 'Red'}).Code\r\n\r\n# Example 3:\r\n# We could also create a hash table and store each item\r\n# from the array using a couple lines of code\r\n$colorCodeHT = @{}\r\n$colorCodes | %{ $colorCodeHT.Add($_.ColorName, $_.Code) }\r\n\r\n# Example 4\r\n# And then we can retreive the code for red using either of the\r\n# examples below. Note: you only need one as they return the same\r\n# value.  The first sample is my preferred method, but either works.\r\n$colorCodeHT['Red']\r\n$colorCodeHT.Item('Red')\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was recently working on a script for a co-worker who is trying to learn more powershell. In the script I had used a couple of hashtables to store information&#8230;which caused a couple of questions as he was reviewing the &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/hashtables-introduction\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1237","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1237"}],"version-history":[{"count":8,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1237\/revisions"}],"predecessor-version":[{"id":1258,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1237\/revisions\/1258"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}