title: PowerShell System.Net.WebClient never closes ftp connection
body: <p>I'm trying to use PowerShell to upload a (long) list of queued files, using <code>System.Net.WebClient</code> and the <code>UploadFile</code> function. This works fine, but after a file us uploaded, the ftp-connection never closes, either when the WebClient object instance goes out of scope or even after the script has finished. The function looks as follows:</p>
  <pre><code>function Upload-File() {
      Param (
          [string] $user,
          [string] $password,
          [string] $srceFileName,
          [string] $destFileName
      )
      # Set up FTP-client
      $client = New-Object System.Net.WebClient
      $client.Credentials = New-Object System.Net.NetworkCredential($user, $password)
      $client.UploadFile($destFileName, &quot;.\$srceFileName&quot;)
      $client.Dispose()
  }
  </code></pre>
  <p>All the information I can find states that the connection should close automatically when <code>$client</code> goes out of scope but this is clearly not happening.</p>
  <p>Any idea on how to force the connection to close?</p>
  <p>(This is part of a legacy system and for now I am stuck with ftp, so switching to another protocol is not an option.)</p>