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.
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
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.
Lets start off by Importing the POSH-SSH module
Lets add the credentials into the $Credential variable
Enter the User Name and Password provided by sftp.net (https://www.sftp.net/public-online-sftp-servers)
Lets check the $Credential variable
Lets use the New-SFTPSession command to connect into the SFTP server
Lets take a look at the items in the root directory
After poking around we found some files in /pub/example
Lets just display only the FullName
Lets filter just the files with the .png file extension
Lets pass all the files into the $SFTPFiles variable
Let create a ForEach loop to get the file then test if it was copied to c:\test directory.
Your output should look like this