π‘ Multiple ways to connect to FTP/FTPS server using PowerShell
To connect and list files on FTP server all you have to do is run 3 line of code
# Anonymous login
$Client = Connect-FTP -Server 'speedtest.tele2.net' -Verbose
$List = Get-FTPList -Client $Client
$List | Format-Table
Disconnect-FTP -Client $Client
If you would like to use username and password (most of the cases)
# Login via UserName/Password
$Client = Connect-FTP -Server 'test.rebex.net' -Verbose -Username 'demo' -Password 'password'
$List = Get-FTPList -Client $Client
$List | Format-Table
Disconnect-FTP -Client $Client
And finally, the same thing can be achieved using credentials that are a bit safer if you want to store the password instead of using the ClearText option safely.
# Login via credentials
$Credential = Get-Credential -UserName 'demo' -Message 'Please enter password' # password is password
$Client = Connect-FTP -Server 'test.rebex.net' -Verbose -Credential $Credential
$List = Get-FTPList -Client $Client
$List | Format-Table
Disconnect-FTP -Client $Client
Connect-FTP cmdlet will use default settings when connecting to a given FTP server. Of course, you can define what settings you want to use.Β All the options such as SSL, Data Connection Type, Encryption Modes, validation of certificates are there.
NAME
Connect-FTP
SYNTAX
Connect-FTP [-Server ] [-Credential ] [-EncryptionMode {None | Implicit | Explicit | Auto}] [-DataConnectionType {AutoPassive | PASV | PASVEX | EPSV | AutoActive | PORT | EPRT}] [-SslBuffering {Auto | Off | On}] [-DisableDataConnec
tionEncryption] [-DisableValidateCertificateRevocation] [-ValidateAnyCertificate] [-Port ] [-SendHost] [-SocketKeepAlive] [-AutoConnect] []
Connect-FTP [-FtpProfile ] []
Connect-FTP [-Server ] [-Username ] [-Password ] [-EncryptionMode {None | Implicit | Explicit | Auto}] [-DataConnectionType {AutoPassive | PASV | PASVEX | EPSV | AutoActive | PORT | EPRT}] [-SslBuffering {Auto | Off | On}] [-Disa
bleDataConnectionEncryption] [-DisableValidateCertificateRevocation] [-ValidateAnyCertificate] [-Port ] [-SendHost] [-SocketKeepAlive] [-AutoConnect] []
ALIASES
None
REMARKS
None
It's also possible to run Request-FTPConfiguration and have the module test for you all different combinations that work with a given FTP server.
# Login via UserName/Password
$ProfileFtp1 = Request-FTPConfiguration -Server 'test.rebex.net' -Verbose -Username 'demo' -Password 'password'
$ProfileFtp1 | Format-Table
# Anonymous login
$ProfileFtp2 = Request-FTPConfiguration -Server 'speedtest.tele2.net' -Verbose
$ProfileFtp2 | Format-Table
Profiles from Request-FTPConfiguration can then be directly used by the Connect-FTP cmdlet.
# Login via UserName/Password via autodetect - keep in mind using Connect-FTP directly will be faster...
$ProfileFtp1 = Request-FTPConfiguration -Server 'test.rebex.net' -Verbose -Username 'demo' -Password 'password'
$ProfileFtp1 | Format-Table
# use first profile
$Client = Connect-FTP -FtpProfile $ProfileFtp1[0]
Get-FTPList -Client $Client | Format-Table
Disconnect-FTP -Client $Client
Or you can simply use the AutoConnect switch, which will go thru those options directly before connecting.
# Login via UserName/Password via autoconnect - this will try all options and connect, while displaying Verbose what settings were used to achieve connection
# Useful for trying out every possible combination
$Client = Connect-FTP -Server 'test.rebex.net' -Username 'demo' -Password 'password' -AutoConnect -Verbose
Get-FTPList -Client $Client | Format-Table
Disconnect-FTP -Client $Client