PowerShell – Get FTP folder file list

PowerShell - Get FTP folder list

Because I’m lazy and/or I don’t have time to format the code, above is a screenshot for clarity, below is the code for whoever needs it.

function Get-FtpFolderFileList([string]$ftpSourceFolder, [string]$ftpUser, [string]$ftpPassword)
    [System.Net.FtpWebRequest]$ftp = [System.Net.WebRequest]::Create($ftpSourceFolder)
    $ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    $ftp.Credentials = New-Object System.Net.NetworkCredential($ftpUser, $ftpPassword)
    $response = $ftp.GetResponse()
    $stream = $response.GetResponseStream()
    $buffer = new-object System.Byte[] 1024
    $encoding = new-object System.Text.UTF8Encoding
    $outputBuffer = “”
    $foundMore = $false
        start-sleep -m 1000
        $foundmore = $false
        $stream.ReadTimeout = 1000
                $read = $stream.Read($buffer, 0, 1024)
                if ($read -gt 0)
                    $foundmore = $true
                    $outputBuffer += ($encoding.GetString($buffer, 0, $read))
            } catch { $foundMore = $false; $read = 0 }
        } while ($read -gt 0)
    } while ($foundmore)
    $fileList = $outputBuffer.Replace(“`r`n”, “`n”).Replace(“`r”, “`n”).Split(“`n”)
    return $fileList

In my case, the $ftpSourceFolder variable had to be like


The symbol %2f it’s an escape slash, and it’s needed when separating the FTP name from the folder with the content.

The files will be retrieved in the short format, only name and extension, but that should not be the problem if you already have the FTP subfolder name

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: