title: PowerShell release COM object
body: <p>I have a small utility function in one PowerShell script:</p>
  
  <pre><code>function Unzip-File{
      param(
          [Parameter(Mandatory=$true)]
          [string]$ZipFile,
          [Parameter(Mandatory=$true)]
          [string]$Path
      )
  
      $shell=New-Object -ComObject shell.application
  
      $zip = $shell.namespace($ZipFile)
      $target = $shell.NameSpace($Path)
      $target.CopyHere($zip.Items())
  }
  </code></pre>
  
  <p>Am I supposed to clean-up the COM objects within the script? Or is PowerShell smart enough to automatically garbage COM Objects?</p>
  
  <p>I've read <a href="http://technet.microsoft.com/en-us/library/ff730962.aspx" rel="noreferrer">Getting Rid of a COM Object (Once and For All)</a> and <em>blindly</em> applied:</p>
  
  <pre><code>[System.Runtime.Interopservices.Marshal]::ReleaseComObject($zip)
  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($target)
  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
  </code></pre>
  
  <p>But I'm not sure if this was needed.</p>
  
  <p>What the correct pattern of using COM objects in local functions?</p>