How to view, upload, download, and delete files from an FTP server using Windows PowerShell

Home > Search > How-to
  by

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 in the box below so that we can be sure you are a human.




Comments