{"id":6931,"date":"2007-11-27T13:31:15","date_gmt":"2007-11-27T13:31:15","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/11\/27\/new-hashtable\/"},"modified":"2019-02-18T13:16:21","modified_gmt":"2019-02-18T20:16:21","slug":"new-hashtable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/new-hashtable\/","title":{"rendered":"New-HashTable"},"content":{"rendered":"<p>John Sheehan the architect for Microsoft Application Virtualization (aka SoftGrid) recently jumped on the PowerShell bandwagon and was nice enough to write up his experiences.&#160; At one point he wrote himself a process monitor which involved putting all the processes into a hashtable indexed by ProcessID.&#160; This is a fairly trivial piece of code to write but I always thought that we ought to provide it for people so I decided to blast one out and share it.<\/p>\n<p>&#160;<\/p>\n<p>This is the code for New-HashTable.ps1<\/p>\n<p><font face=\"Courier New\" size=\"2\"># FILE:&#160;&#160;&#160;&#160; New-HashTable.ps1     <br \/># Author:&#160;&#160; Jeffrey Snover      <br \/># Version:&#160; 0.1      <br \/>#Requires -Version 1.0      <br \/>param(      <br \/>&#160;&#160;&#160; $Key=$(Throw &quot;USAGE: New-HashTable -Key &lt;property&gt;&quot;),      <br \/>&#160;&#160;&#160; [Switch]$NoOverWrite,      <br \/>&#160;&#160;&#160; [Switch]$MakeArray      <br \/>&#160;&#160;&#160; )      <br \/>Begin      <br \/>{      <br \/>&#160;&#160;&#160; $hash = @{}      <br \/>}      <br \/>Process      <br \/>{&#160; <br \/>&#160;&#160;&#160; $Property = $_.$key      <br \/>&#160;&#160;&#160; if ($NoOverWrite -And $hash.$Property)      <br \/>&#160;&#160;&#160; {&#160;&#160; Write-Error &quot;$Property already exists&quot;      <br \/>&#160;&#160;&#160; }elseif ($MakeArray)      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!$hash.$Property)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160; $hash.$Property = new-object System.Collections.ArrayList      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [void]$hash.$Property.Add($_)      <br \/>&#160;&#160;&#160; }else      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; $hash.$Property = $_      <br \/>&#160;&#160;&#160; }      <br \/>}      <br \/>End      <br \/>{      <br \/>&#160;&#160;&#160; $hash      <br \/>}<\/font><\/p>\n<p>&#160;<\/p>\n<p>Here it is running:<\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;<strong>gps *ss |new-hashtable       <br \/><\/strong><font color=\"#ff0000\">USAGE: New-HashTable -Key &lt;property&gt;       <br \/>At c:\\ps\\New-HashTable.ps1:6 char:17        <br \/>+&#160;&#160;&#160;&#160; $Key=$(Throw &lt;&lt;&lt;&lt;&#160; &quot;USAGE: New-HashTable -Key &lt;property&gt;&quot;),        <br \/><\/font><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;<strong>gps *ss |new-hashtable -key id<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Value     <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8211;      <br \/>516&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 516 &#8211; csrss      <br \/>620&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 620 &#8211; lsass      <br \/>576&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 576 &#8211; csrss      <br \/>388&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 388 &#8211; smss <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\"><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;<strong>gps *ss |new-hashtable -key product <\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Value     <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8211;      <br \/>Microsoft&#174; Windows&#174; Operati&#8230; 388 &#8211; smss <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\"><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;<strong>gps *ss |new-hashtable -key product -NoOverWrite<\/strong>      <br \/><font color=\"#ff0000\">c:\\ps\\New-HashTable.ps1 : Microsoft&#174; Windows&#174; Operating System already exists       <br \/>At line:1 char:23        <br \/>+ gps *ss |new-hashtable &lt;&lt;&lt;&lt;&#160; -key product -NoOverWrite        <br \/>c:\\ps\\New-HashTable.ps1 : Microsoft&#174; Windows&#174; Operating System already exists        <br \/>At line:1 char:23        <br \/>+ gps *ss |new-hashtable &lt;&lt;&lt;&lt;&#160; -key product -NoOverWrite        <br \/>c:\\ps\\New-HashTable.ps1 : Microsoft&#174; Windows&#174; Operating System already exists        <br \/>At line:1 char:23        <br \/>+ gps *ss |new-hashtable &lt;&lt;&lt;&lt;&#160; -key product -NoOverWrite <\/font><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\"><font color=\"#ff0000\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Value       <br \/><\/font>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8211;      <br \/>Microsoft&#174; Windows&#174; Operati&#8230; 516 &#8211; csrss <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\"><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;<strong>gps *ss |new-hashtable -key product -MakeArray<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Value     <br \/>&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8211;      <br \/>Microsoft&#174; Windows&#174; Operati&#8230; {516 &#8211; csrss, 576 &#8211; csrss, 620 &#8211; lsass, 388 &#8211; smss}<\/font><\/p>\n<p><font face=\"Consolas\"><\/font><\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Windows Management Partner Architect    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>John Sheehan the architect for Microsoft Application Virtualization (aka SoftGrid) recently jumped on the PowerShell bandwagon and was nice enough to write up his experiences.&#160; At one point he wrote himself a process monitor which involved putting all the processes into a hashtable indexed by ProcessID.&#160; This is a fairly trivial piece of code to [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>John Sheehan the architect for Microsoft Application Virtualization (aka SoftGrid) recently jumped on the PowerShell bandwagon and was nice enough to write up his experiences.&#160; At one point he wrote himself a process monitor which involved putting all the processes into a hashtable indexed by ProcessID.&#160; This is a fairly trivial piece of code to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6931"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}