title: Merging hashtables in PowerShell: how?
body: <p>I am trying to merge two hashtables, overwriting key-value pairs in the first if the same key exists in the second.</p>
  
  <p>To do this I wrote this function which first removes all key-value pairs in the first hastable if the same key exists in the second hashtable.</p>
  
  <p>When I type this into PowerShell line by line it works. But when I run the entire function, PowerShell asks me to provide (what it considers) missing parameters to foreach-object.</p>
  
  <pre><code>function mergehashtables($htold, $htnew)
  {
      $htold.getenumerator() | foreach-object
      {
          $key = $_.key
          if ($htnew.containskey($key))
          {
              $htold.remove($key)
          }
      }
      $htnew = $htold + $htnew
      return $htnew
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>PS C:\&gt; mergehashtables $ht $ht2
  
  cmdlet ForEach-Object at command pipeline position 1
  Supply values for the following parameters:
  Process[0]:
  </code></pre>
  
  <p>$ht and $ht2 are hashtables containing two key-value pairs each, one of them with the key "name" in both hashtables.</p>
  
  <p>What am I doing wrong?</p>