title: Updating hash table values in a 'foreach' loop in PowerShell
body: <p>I'm trying to loop through a hash table and set the value of each key to 5 and PowerShell gives an error:</p>
  
  <pre><code>$myHash = @{}
  $myHash["a"] = 1
  $myHash["b"] = 2
  $myHash["c"] = 3
  
  foreach($key in $myHash.keys){
      $myHash[$key] = 5
  }
  </code></pre>
  
  <p>An error occurred while enumerating through a collection:</p>
  
  <pre><code>Collection was modified; enumeration operation may not execute..
  At line:1 char:8
  + foreach &lt;&lt;&lt;&lt; ($key in $myHash.keys){
      + CategoryInfo          : InvalidOperation: (System.Collecti...tableEnumer
     ator:HashtableEnumerator) [], RuntimeException
      + FullyQualifiedErrorId : BadEnumeration
  </code></pre>
  
  <p>What gives and how do I resolve this problem?</p>