PowerShell Script to upload an entire folder to FTP PowerShell Script to upload an entire folder to FTP powershell powershell

PowerShell Script to upload an entire folder to FTP


The loop (or even better a recursion) is the only way to do this natively in PowerShell (or .NET in general).

$source = "c:\source"$destination = "ftp://username:password@example.com/destination"$webclient = New-Object -TypeName System.Net.WebClient$files = Get-ChildItem $sourceforeach ($file in $files){    Write-Host "Uploading $file"    $webclient.UploadFile("$destination/$file", $file.FullName)} $webclient.Dispose()

Note that the above code does not recurse into subdirectories.


If you need a simpler solution, you have to use a 3rd party library.

For example with WinSCP .NET assembly:

Add-Type -Path "WinSCPnet.dll"$sessionOptions = New-Object WinSCP.SessionOptions$sessionOptions.ParseUrl("ftp://username:password@example.com/")$session = New-Object WinSCP.Session$session.Open($sessionOptions)$session.PutFiles("c:\source\*", "/destination/").Check()$session.Dispose()

The above code does recurse.

See https://winscp.net/eng/docs/library_session_putfiles

(I'm the author of WinSCP)