Using FTP: Listing Folders (Part 1)

by Aug 27, 2021

PowerShell does not come with cmdlets to download and upload data via FTP. However, you can use .NET for this.

To display the contents of an FTP folder, try using this code:

$username='testuser'
$password='P@ssw0rd'
$ftp='ftp://192.168.1.123'
$subfolder='/'

[System.Uri]$uri = $ftp + $subfolder
$ftprequest=[System.Net.FtpWebRequest]::Create($uri)
$ftprequest.Credentials= [System.Net.NetworkCredential]::new($username,$password)
$ftprequest.Method=[System.Net.WebRequestMethods+Ftp]::ListDirectory
$response=$ftprequest.GetResponse()
$stream=$response.GetResponseStream()
$reader=[System.IO.StreamReader]::new($stream,[System.Text.Encoding]::UTF8)
$content=$reader.ReadToEnd()

$content 


Twitter This Tip! ReTweet this Tip!