title: Deleting file from FTP server using PowerShell
body: <p>I wrote a PowerShell script to download files using FTPto my local machine. </p>
  
  <p>After the file is downloaded, I want to delete it from the FTP server. I wrote this code too. But unfortunately it's not working. </p>
  
  <p>Can anyone help me to point out what is wrong with my code? Any clues will be helpful ...</p>
  
  <p><strong>Here is my code</strong></p>
  
  <pre><code>function Delete-File($Source,$Target,$UserName,$Password)
  {
  
      $ftprequest = [System.Net.FtpWebRequest]::create($Source)
      $ftprequest.Credentials =  New-Object System.Net.NetworkCredential($UserName,$Password)
  
      if(Test-Path $Source)
      {
         "ABCDEF File exists on ftp server."
         $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
         $ftprequest.GetResponse()
  
         "ABCDEF File deleted."
      }
  
  }
  
  function Get-FTPFile ($Source,$Target,$UserName,$Password)  
  {  
  
      # Create a FTPWebRequest object to handle the connection to the ftp server  
      $ftprequest = [System.Net.FtpWebRequest]::create($Source)  
  
      # set the request's network credentials for an authenticated connection  
      $ftprequest.Credentials =  
      New-Object System.Net.NetworkCredential($username,$password)  
      if(Test-Path $targetpath)
      {
          "ABCDEF File exists"
      }
      else 
      { 
          "ABCDEF File downloaded"
           $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile  
  
           $ftprequest.UseBinary = $true  
           $ftprequest.KeepAlive = $false  
           Delete-File $sourceuri $targetpath $user $pass
      }
  
      # send the ftp request to the server  
      $ftpresponse = $ftprequest.GetResponse()  
  
      # get a download stream from the server response  
      $responsestream = $ftpresponse.GetResponseStream()  
  
      # create the target file on the local system and the download buffer  
      $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create)  
      [byte[]]$readbuffer = New-Object byte[] 1024  
  
      # loop through the download stream and send the data to the target 
      file  
      do{  
            $readlength = $responsestream.Read($readbuffer,0,1024)  
            $targetfile.Write($readbuffer,0,$readlength)  
      }  
      while ($readlength -ne 0)  
  
      $targetfile.close()  
  }  
  
  $sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML"  
  $targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML"  
  $user = "*******"  
  $pass = "*******"  
  Get-FTPFile $sourceuri $targetpath $user $pass 
  Delete-File $sourceuri $targetpath $user $pass
  </code></pre>
  
  <p>Every time I execute this script, the only statement I get</p>
  
  <blockquote>
    <p>ABCDEF file downloaded </p>
  </blockquote>
  
  <p>or </p>
  
  <blockquote>
    <p>ABCDEF file exists</p>
  </blockquote>
  
  <p>I guess <code>Delete-File</code> is not executing at all... any type of clue will be helpful.</p>