title: PowerShell FTP download files and subfolders
body: <p>I like to write a PowerShell script to download <strong>all files</strong> and <strong>subfolders</strong> from my FTP server. I found a script to download all files from one specific folder, but I also like to download the subfolders and their files.</p>
  
  <pre class="lang-js prettyprint-override"><code>#FTP Server Information - SET VARIABLES
  $ftp = "ftp://ftp.abc.ch/" 
  $user = 'abc' 
  $pass = 'abc'
  $folder = '/'
  $target = "C:\LocalData\Powershell\"
  
  #SET CREDENTIALS
  $credentials = new-object System.Net.NetworkCredential($user, $pass)
  
  function Get-FtpDir ($url,$credentials) {
      $request = [Net.WebRequest]::Create($url)
      $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
      if ($credentials) { $request.Credentials = $credentials }
      $response = $request.GetResponse()
      $reader = New-Object IO.StreamReader $response.GetResponseStream() 
      $reader.ReadToEnd()
      $reader.Close()
      $response.Close()
  }
  
  #SET FOLDER PATH
  $folderPath= $ftp + "/" + $folder + "/"
  
  $Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
  $files = ($Allfiles -split "`r`n")
  
  $files 
  
  $webclient = New-Object System.Net.WebClient 
  $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
  $counter = 0
  foreach ($file in ($files | where {$_ -like "*.*"})){
      $source=$folderPath + $file  
      $destination = $target + $file 
      $webclient.DownloadFile($source, $target+$file)
  
      #PRINT FILE NAME AND COUNTER
      $counter++
      $counter
      $source
  }
  </code></pre>
  
  <p>Thanks for your help (:</p>