The following PowerShell script demonstrates how to get the list of files and folders in a specific directory on a FTP server using the FTPWebRequest class of System.Net namespace. The FTPWebRequest class implements a FTP Client which allows you to communicate with an FTP server programmatically.
At first you create an instance of the FTPWebRequest class by passing a URI to the create method call. The URI must be in the format:
ftp://servername/directorypath
The FTP client must send a username and password to the host to authenticate any requests made. To do this you create a new object of the NetworkCredential class with the username and password strings as a parameters. This credential object is assigned to the Credentials property of the FTPWebRequest class.
The next step is to set the protocol method to ListDirectoryDetails which gets a detailed listing of files and folders on the FTP server. If you only need a short listing then set the method to ListDirectory
The GetResponse method returns an instance of the FTPWebResponse class. The data stream of this response object contains the response data (directory listing) received from the FTP Server. You can get the data stream by calling the GetResponseStream() method and then using a stream reader to read the content of the stream.
Finally you can use the ReadLine inside a loop to read all the data until the end of the stream.
In the below script, the core logic to get the directory listing from a FTP host is wrapped inside a function named Get-FTPFileList
. FTP host name, user name, password and the directory path is passed to this function as parameters when it is called. On successful completion, Get-FTPFileList function returns and array list containing the directory list.
# Function to get directory listing Function Get-FTPFileList { Param ( [System.Uri]$server, [string]$username, [string]$password, [string]$directory ) try { #Create URI by joining server name and directory path $uri = "$server$directory" #Create an instance of FtpWebRequest $FTPRequest = [System.Net.FtpWebRequest]::Create($uri) #Set the username and password credentials for authentication $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($username, $password) #Set method to ListDirectoryDetails to get full list #For short listing change ListDirectoryDetails to ListDirectory $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails #Get FTP response $FTPResponse = $FTPRequest.GetResponse() #Get Reponse data stream $ResponseStream = $FTPResponse.GetResponseStream() #Read data Stream $StreamReader = New-Object System.IO.StreamReader $ResponseStream #Read each line of the stream and add it to an array list $files = New-Object System.Collections.ArrayList While ($file = $StreamReader.ReadLine()) { [void] $files.add("$file") } } catch { #Show error message if any write-host -message $_.Exception.InnerException.Message } #Close the stream and response $StreamReader.close() $ResponseStream.close() $FTPResponse.Close() Return $files } #Set server name, user, password and directory $server = 'ftp://servername/' $username = 'ftp_user' $password = 'ftp_passwd' $directory ='directory_path' #Function call to get directory listing $filelist = Get-FTPFileList -server $server -username $username -password $password -directory $directory #Write output Write-Output $filelist
The format of the directory listing can be different depending on the FTP server software that you are connecting to. Given below is a sample output from a FTP server hosted on a Windows 10 machine.
12-03-17 07:28PM 83730 BN-RF812_dollar_TOP_20161215063923.jpg 12-05-17 03:23PM 48388 ea3503ed.gif 03-03-16 12:12PM 5876 flexbox.html 01-09-18 12:34PM <DIR> sub 12-23-16 03:42PM 3265 wifi.txt