title: Variable scoping in PowerShell
body: <p>A sad thing about PowerShell is that function and scriptblocks are dynamically scoped.</p>
  
  <p>But there is another thing that surprised me is that variables behave as a copy-on-write within an inner scope.</p>
  
  <pre><code>$array=@("g")
  function foo()
  {
      $array += "h"
      Write-Host $array
  }
  
  &amp; {
      $array +="s"
      Write-Host $array
  }
  foo
  
  Write-Host $array
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>g s
  g h
  g
  </code></pre>
  
  <p>Which makes dynamic scoping a little bit less painful. But how do I avoid the copy-on-write?</p>