FTP with PowerShell

FTP with PowerShell

14 November 2019 0 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:

[cc lang=”powershell” tab_size=”2″ lines=”40″]

Install-Module PSFTP

[/cc]

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

[cc lang=”powershell” tab_size=”2″ lines=”40″]

Import-Module PSFTP

[/cc]

Script to connect to the FTP server

[cc lang=”powershell” tab_size=”2″ lines=”40″]

$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 “/”

[/cc]

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: 5499