FTP with PowerShell

14 November 2019 Off By Rached CHADER

We will use a PowerShell module available for download at https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb
Unpack the module in C:\WINDOWS\System32\WindowsPowerShell\v1.0\Modules\

From a PowerShell console, install the module from the PowerShell Gallery:

1
Install-Module PSFTP

In my case the module is already present, so to have the new version I made install-module -force

1
Import-Module PSFTP

Script to connect to the FTP server

1
$FTPServer = 'FTP.Server'$FTPUsername = 'username'$FTPPassword = 'password'$FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)  Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session MySession -UsePassive $Session = Get-FTPConnection -Session MySession  Get-FTPChildItem -Session $Session -Path "/"

Get-FTPChildItem allows you to list the contents in the root directory of the FTP.

Once connected you can upload a file via the following command:

Get-ChildItem “C:\chader\test.txt” | Add-FTPItem -Session $Session -Path /FTP/

Views: 5321