How to view the files on an FTP server using Windows PowerShell

Home > Search > How-to
  by

To view the files on an 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
$folderPath= $FTPServer + "/" + $folder + "/"
$ListFiles=View-Files-in-FTP-Server -url $folderPath -credentials $credentials
$files = ($ListFiles -split "`r`n")
$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