Bootstrap FreeKB - PowerShell - Upload, download, and delete files from an FTP server
PowerShell - Upload, download, and delete files from an FTP server

Updated:   |  PowerShell articles

To upload a single file, use the UploadFile method.

#Unique Variables
$FTPServer = "ftp://example.com/"
$FTPUsername = "username"
$FTPPassword = "password" 
$LocalDirectory = "C:\temp\" 
$FileToUpload = "example.txt"

#Connect to the FTP Server
$ftp = [System.Net.FtpWebRequest]::create("$FTPServer/$FileToUpload")
$ftp.Credentials =  New-Object System.Net.NetworkCredential($FTPUsername,$FTPPassword)

#Upload file to FTP Server
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

#Verify the file was uploaded
$ftp.GetResponse()

 

To delete a single file, use the DeleteFile method.

#Unique Variables
$FTPServer = "ftp://example.com/"
$FTPUsername = "username"
$FTPPassword = "password" 
$LocalDirectory = "C:\temp\" 
$FileToDelete = "example.txt"

#Connect to the FTP Server
$ftp = [System.Net.FtpWebRequest]::create("$FTPServer/$FileToDelete")
$ftp.Credentials =  New-Object System.Net.NetworkCredential($FTPUsername,$FTPPassword)

#Delete file on FTP Server
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile

#Verify the file was uploaded
$ftp.GetResponse()

 

To upload every file in a directory:

#Unique Variables
$FTPServer = "ftp://example.com/"
$FTPUsername = "username"
$FTPPassword = "password" 
$LocalDirectory = "C:\temp\" 

#Loop through every file
foreach($FileToUpload in (dir $LocalDirectory "*")){ 

    #Connect to the FTP Server
    $ftp = [System.Net.FtpWebRequest]::create("$FTPServer/$FileToUpload")
    $ftp.Credentials =  New-Object System.Net.NetworkCredential($FTPUsername,$FTPPassword)

    #Upload file to FTP Server
    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

    #Verify the file was uploaded
    $ftp.GetResponse()
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


January 05 2021 by Vittorio Marino
Excellent !!! Finally I found , after long surfing in the web the rigth PS to put files in FTP

June 08 2021 by Avi Siboni
Hi, Thank you for sharing these great examples, is there any way to capture a specific error(something like catch type?)

January 09 2023 by Santanu Ghosh
Hello, it is not working, getting below error, Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (553) File name not allowed." At C:\TEST1\FTPupload4.ps1:16 char:1 + $ftp.GetResponse() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException ========================= Also, from where you are using the variable $LocalDirectory in the code?

February 22 2023 by Meh
Doesn't work. You never specify the local directory/filetoupload anywhere but in the variable definitions. It has a target but nothing to send,

Add a Comment


Please enter 6f3e17 in the box below so that we can be sure you are a human.