{"id":3594,"date":"2013-05-21T00:01:00","date_gmt":"2013-05-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/21\/powershell-hash-tables-that-contain-keywords-hmm\/"},"modified":"2013-05-21T00:01:00","modified_gmt":"2013-05-21T00:01:00","slug":"powershell-hash-tables-that-contain-keywords-hmm","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-hash-tables-that-contain-keywords-hmm\/","title":{"rendered":"PowerShell Hash Tables that Contain Keywords&#8230;hmm&#8230;"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about dealing with Windows PowerShell hash tables that contain keywords. Dude!<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! I have a problem in that I want to create a hash table from an array. This is easy enough to do, but if the array contains keywords, then everything goes pear shaped. Any ideas?\n&mdash;GP\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello GP,\nMicrosoft Scripting Guy, Ed Wilson, is here. Things are certainly&nbsp; becoming exciting around here. The Scripting Wife and I have been busy planning for TechEd North America in New Orleans and TechEd Europe in Madrid. As it turns out, we will have a Scripting Guys booth in both New Orleans and Madrid. In addition, we are sharing our booth with the folks from PowerShell.org; so, that means that Windows PowerShell people like Don Jones will be there. I am also going to be working with the Windows PowerShell team on a couple of instructor lead labs that (to be quite honest) will absolutely knock your socks off &mdash;they are that cool. More details to follow.<\/p>\n<h2>Creating a hash table that contains keywords<\/h2>\n<p>Well GP, I have never actually run across your problem before. This is because I avoid using Windows PowerShell keywords. I refer to the Help topic,&nbsp;<a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847744.aspx\" target=\"_blank\">about_Language_Keywords<\/a>, from time to time to refresh my mind when it comes to reserved keywords.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp; As an aside, I do the same thing for automatic variables. For more information, see <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847768.aspx\" target=\"_blank\">about_Automatic_Variables<\/a> in the Script Center.&nbsp;\nHere is the easy way to bring up the about_Language_Keywords Help topic:<\/p>\n<p style=\"padding-left: 30px\">help -Category helpfile -Name *keyword*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\nSo, this is all on the preventive side of things. To experiment with this, the first thing I do is create an array that contains a few keywords&mdash;in addition to other words. I then use the <strong>ForEach<\/strong><em> <\/em>command to add the items in the array to a hash table. This technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $a = &#8220;key&#8221;,&#8221;keys&#8221;,&#8221;property&#8221;,&#8221;properties&#8221;,&#8221;red&#8221;,&#8221;blue&#8221;,&#8221;green&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h = @{}<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; foreach ($wd in $a) {$h[$wd] = $true}\nIf I look at the hash table that is contained in the <strong>$h<\/strong> variable, I see the following:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h<\/p>\n<p style=\"padding-left: 30px\">&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;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">red&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; True<\/p>\n<p style=\"padding-left: 30px\">property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True<\/p>\n<p style=\"padding-left: 30px\">green&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; True<\/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; True<\/p>\n<p style=\"padding-left: 30px\">properties&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True<\/p>\n<p style=\"padding-left: 30px\">blue&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; True<\/p>\n<p style=\"padding-left: 30px\">key&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; True\nSo far, so good.&nbsp; But suppose I want to look at the properties or the keys of my hash table. I try the following:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h.Keys<\/p>\n<p style=\"padding-left: 30px\">True<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h.key<\/p>\n<p style=\"padding-left: 30px\">True<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h.property<\/p>\n<p style=\"padding-left: 30px\">True<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h.properties<\/p>\n<p style=\"padding-left: 30px\">True\nSo I am wondering what the properties really are for this hash table. I can find out by using the <strong>Get-Member<\/strong> command as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-Member -InputObject $h -MemberType Properties<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.Collections.Hashtable<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; int Count {get;}<\/p>\n<p style=\"padding-left: 30px\">IsFixedSize&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; bool IsFixedSize {get;}<\/p>\n<p style=\"padding-left: 30px\">IsReadOnly&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; bool IsReadOnly {get;}<\/p>\n<p style=\"padding-left: 30px\">IsSynchronized Property&nbsp;&nbsp; bool IsSynchronized {get;}<\/p>\n<p style=\"padding-left: 30px\">Keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Collections.ICollection Keys {get;}<\/p>\n<p style=\"padding-left: 30px\">SyncRoot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Object SyncRoot {get;}<\/p>\n<p style=\"padding-left: 30px\">Values&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Collections.ICollection Values {get;}\n<span style=\"font-size: 12px\">There really is a property called <\/span><strong style=\"font-size: 12px\">Keys<\/strong><em style=\"font-size: 12px\">, <\/em><span style=\"font-size: 12px\">and I really cannot seem to access it. So what do I do?<\/span>\nI imagine that I will take a look at the base Windows PowerShell object. To do this, I use the <strong>PSBase<\/strong><em> <\/em>property. All Windows PowerShell objects have a base object, but most of the time, it is not necessary to fool with it. Here, however, I suspect that I will be able to get past the <em>keys keys <\/em>kind of thing. Here is the base object:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h.psbase<\/p>\n<p style=\"padding-left: 30px\">IsReadOnly&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">IsFixedSize&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">IsSynchronized : False<\/p>\n<p style=\"padding-left: 30px\">Keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {red, property, green, keys&#8230;}<\/p>\n<p style=\"padding-left: 30px\">Values&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {True, True, True, True&#8230;}<\/p>\n<p style=\"padding-left: 30px\">SyncRoot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : System.Object<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 7\nNow, I see there is a <strong>Keys<\/strong><em> <\/em>property on the base object. So I attempt to access it. Here is how I do it:<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $h.psbase.keys<\/p>\n<p style=\"padding-left: 30px\">red<\/p>\n<p style=\"padding-left: 30px\">property<\/p>\n<p style=\"padding-left: 30px\">green<\/p>\n<p style=\"padding-left: 30px\">keys<\/p>\n<p style=\"padding-left: 30px\">properties<\/p>\n<p style=\"padding-left: 30px\">blue<\/p>\n<p style=\"padding-left: 30px\">key\nGP, that is all there is to using <strong>PSBase<\/strong> when dealing with hash tables. By the way, I tested this on Windows PowerShell&nbsp;2.0 and on Windows PowerShell&nbsp;3.0, so you should be safe to use this technique. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto: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.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about dealing with Windows PowerShell hash tables that contain keywords. Dude! &nbsp;Hey, Scripting Guy! I have a problem in that I want to create a hash table from an array. This is easy enough to do, but if the array contains keywords, then everything goes pear shaped. Any [&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":[427,3,4,45],"class_list":["post-3594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hashtables","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about dealing with Windows PowerShell hash tables that contain keywords. Dude! &nbsp;Hey, Scripting Guy! I have a problem in that I want to create a hash table from an array. This is easy enough to do, but if the array contains keywords, then everything goes pear shaped. Any [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3594","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=3594"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3594\/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=3594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}