top of page
Search

How to SFTP using Powershell

I have a few projects where I need to pull files from a SFTP server into a Windows network share. In this post we will go over how to use POSH-SSH and the SFPT functions.


GitHub: https://github.com/darkoperator/Posh-SSH


Installing POSH-SSH


ree

Open Powershell as Administrator and run the below command:


Find-Module Posh-SSH | Install-Module














Lets now take a look at all the new functions that got added by entering the Get-Command -Module posh-ssh | ft -AutoSize


ree

Now that we have POSH-SSH installed we can try out some of the commands. I found a free SFTP site (https://www.sftp.net/public-online-sftp-servers) that allows you to test out SFTP.



ree

Lets start off by Importing the POSH-SSH module






ree

Lets add the credentials into the $Credential variable



ree

Enter the User Name and Password provided by sftp.net (https://www.sftp.net/public-online-sftp-servers)













ree

Lets check the $Credential variable






ree

Lets use the New-SFTPSession command to connect into the SFTP server






ree

Lets take a look at the items in the root directory













ree

After poking around we found some files in /pub/example


















ree

Lets just display only the FullName










ree

Lets filter just the files with the .png file extension








ree

Lets pass all the files into the $SFTPFiles variable















ree

Let create a ForEach loop to get the file then test if it was copied to c:\test directory.



ree

Your output should look like this









ree

Full code:



ree

 
 
 

Comments


bottom of page