FreeKB - PowerShell View, upload, download, and delete files from an FTP server
PowerShell - View, upload, download, and delete files from an FTP server

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()
}

 

To view the files on the FTP server:

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

#Connection String
$credentials = new-object System.Net.NetworkCredential($FTPUsername, $FTPPassword)

#function to view the files in the FTP Server
function View-Files-in-FTP-Server ($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()
}

#Use the root directory of the FTP Server
$ListFiles = View-Files-in-FTP-Server -url $FTPServer -credentials $credentials
$files = ($ListFiles -split "`r`n")

Write-Host 'The Files on the FTP Server : '
Write-Host `r
$files 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments


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


June 8th, 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?)

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |