Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload files with FTP using PowerShell

Tags:

powershell

ftp

I want to use PowerShell to transfer files with FTP to an anonymous FTP server. I would not use any extra packages. How?

like image 289
magol Avatar asked Dec 08 '09 14:12

magol


People also ask

Can you use PowerShell for FTP?

FTP download file using PowerShell And this is how you can download files from an FTP server using PowerShell. So let me know how FTP file transfers with PowerShell are working for you. Scenarios I have used this are simple automation tasks where we needed to upload or download some files.


1 Answers

I am not sure you can 100% bullet proof the script from not hanging or crashing, as there are things outside your control (what if the server loses power mid-upload?) - but this should provide a solid foundation for getting you started:

# create the FtpWebRequest and configure it $ftp = [System.Net.FtpWebRequest]::Create("ftp://localhost/me.png") $ftp = [System.Net.FtpWebRequest]$ftp $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile $ftp.Credentials = new-object System.Net.NetworkCredential("anonymous","anonymous@localhost") $ftp.UseBinary = $true $ftp.UsePassive = $true # read in the file to upload as a byte array $content = [System.IO.File]::ReadAllBytes("C:\me.png") $ftp.ContentLength = $content.Length # get the request stream, and write the bytes into it $rs = $ftp.GetRequestStream() $rs.Write($content, 0, $content.Length) # be sure to clean up after ourselves $rs.Close() $rs.Dispose() 
like image 122
Goyuix Avatar answered Oct 01 '22 19:10

Goyuix