Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Powershell FTPS Upload Example
param (
[string]$file = $(throw "-file is required"),
[string]$ftphostpath = $(throw "-ftphostpath is required"),
[string]$username = $(throw "-username is required"),
[string]$password = $(throw "-password is required")
$f = dir $file
$req = [System.Net.FtpWebRequest]::Create("ftp://$ftphostpath/" + $f.Name);
$req.Credentials = New-Object System.Net.NetworkCredential($username, $password);
$req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile;
$req.EnableSsl = $True;
$req.UseBinary = $True
$req.UsePassive = $True
$req.KeepAlive = $True
$req.ConnectionGroupName = "FTPS$username";
$fs = new-object IO.FileStream $f.FullName, 'Open', 'Read'
$ftpStream = $req.GetRequestStream();
$req.ContentLength = $f.Length;
try {
$b = new-object Byte[](10000)
while($true) {
$r = $fs.Read($b, 0, 10000);
if($r -eq 0) { break; }
$ftpStream.Write($b, 0, $r);
} finally {
if ($fs -ne $null) { $fs.Dispose(); }
$resp = $req.GetResponse();

This comment has been minimized.

Copy link

@ukreddy-erwin ukreddy-erwin commented Sep 23, 2020

Hi, I need to upload complete folder(only folder contents) to the ftps.
CAn you suggest


This comment has been minimized.

Copy link
Owner Author

@pbrumblay pbrumblay commented Sep 23, 2020

Holy cow! I wrote this 7 years ago. Let's see... assuming it still works (I have moved on from .NET / Powershell to other technologies) you could take a folder name as a parameter on line 2 instead of a single file and then loop over it (something like this: to upload each file to the FTPS server. I hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment