Interacting with FTP/SFTP using Powershell

I’ve been migrating several FTP jobs from batch scripts into PowerShell by leveraging PSFTP – below is a quick guide on how you can do the same.

First, you need to download the PSFTP module –; once downloaded, you need to extract it into your PowerShell modules directory, depending on your OS and environment path configuration, the Modules directory could vary but mine was in C:\Windows\System32\WindowsPowerShell\v1.0\Modules\.

You can also try running $env:PSModulePath.split(;) to list all of the PowerShell modules paths on your system.

Once you have the module extracted, try to import it in a PowerShell window by running – Import-Module “C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSFTP\PSFTP.psm1”. If you get no error messages, then the import worked!

You can then run Get-Command -Module PSFTP to see all the cmdlets available under that module:

Each cmdlet is pretty self-explanatory but you can utilize the Get-Help cmdlet to get more info on each cmdlet:

You can utilize each cmdlet and write a script to accomplish your FTP/SFTP needs, below are a few functions I created to accomplish some common tasks and by writing them as functions, you can easily reuse them from one script to another:

Connect to an FTP Server:

OK, so I’m connected, right? How do I know I’m really connected to the FTP server after running the above function?

Here is the same function from earlier with a validation step to another function to verify if the connection was successful and then perform some action if the connection was successful:

What if you want to upload a file from local storage to the FTP site?

We can easily include Add-FTPItem with our existing function(s) to look something like this:

Besides PSFTP, you can also utilize WinSCP to fulfill your FTP/SFTP requirements within PowerShell.

First, you have to download the WinSCP application, you don’t have to install it, but you need the file directory saved somewhere accessible to your script. You can download WinSCP here –

WinSCP on its own can automate and handle FTP/SFTP files; but if you need to use WinSCP within PowerShell, then you can accomplish it by calling the WinSCP COM object by referencing it with an & symbol in your PowerShell script like this “& “C:\Program Files (x86)\WinSCP\” /command”

With this syntax, you can now leverage any of the WinSCP native commands mentioned here –

Connect to server:
Once you establish the connection successfully, you actually get a WinSCP prompt in your console and all of the mentioned native WinSCP commands are now available to you within that prompt.

The same way that we evaluated if we were able to connect successfully with ConnectFTP using PSFTP, we can do similarly with our WinSCP command(s) by assigning the output of a command into a variable:




Leave a Reply

Be the First to Comment!

Notify of