business.com receives compensation from some of the companies listed on this page. Advertising Disclosure

Home

Managing Files over SFTP with PowerShell

Adam Bertram
business.com Contributing Writer
Sep 05, 2018

Native support for Secure File Transfer Protocol (SFTP) is not in PowerShell.

Although native support for Secure File Transfer Protocol (SFTP) is not in PowerShell, by using a free module, we can retrieve, delete and add new files.

The module we need to use is called Posh-SSH. This module is available on the PowerShell Gallery and can be installed by running Install-Module -Name Posh-SSH. Once installed, we can take a look at all of the available commands it now gives us:

PS C:> Get-Command -Module posh-ssh -Noun *SFTP*

CommandType     Name                                     Version     Source
-----------              ----                                       -------      ------
Function              Get-SFTPChildItem                    2.0.1      posh-ssh
Function              Get-SFTPContent                       2.0.1      posh-ssh
Function              Get-SFTPLocation                      2.0.1      posh-ssh
Function              Get-SFTPPathAttribute               2.0.1      posh-ssh
Function             Get-SFTPSession                        2.0.1      posh-ssh
Function              New-SFTPFileStream                 2.0.1      posh-ssh
Function              New-SFTPItem                          2.0.1      posh-ssh
Function              New-SFTPSymlink                      2.0.1     posh-ssh
Function              Remove-SFTPItem                    2.0.1      posh-ssh
Function              Remove-SFTPSession                2.0.1      posh-ssh
Function             Rename-SFTPFile                       2.0.1      posh-ssh
Function             Set-SFTPContent                       2.0.1       posh-ssh
Function             Set-SFTPLocation                       2.0.1      posh-ssh
Function             Set-SFTPPathAttribute                2.0.1      posh-ssh
Function              Test-SFTPPath                          2.0.1      posh-ssh
Cmdlet                Get-SFTPFile                             2.0.1      posh-ssh
Cmdlet               New-SFTPSession                       2.0.1      posh-ssh
Cmdlet               Set-SFTPFile                              2.0.1      posh-ssh

Right away, it looks like can do a lot with this module. Let's dig into some of the SFTP commands and see how they can be used.

Options for the model

To transfer files over SFTP, we need to establish a session to the SFTP server. We'll only have to do this once. We could create a separate session each time we need to perform some task over SFTP, but that wouldn't be too efficient.

Instead, let's create a single SFTP session using the New-SFTPSession command. We're going to take the easy route and not mess with certificates so we can use a username and password to authenticate to the SFTP server. The New-SFTPSession has a Credential parameter that accepts a PSCredential object. We'll use the Get-Credential command to prompt the user for a username and password.

$credential = Get-Credential

Once we've got the username and password capture, we can then pass that to the New-SFTPSession command along with the AcceptKey parameter. The AcceptKey parameter will automatically accept the key that's returned from the SFTP server rather than prompting you to do so.

$session = New-SFTPSession -ComputerName 'MYSFTPSERVER' -Credential $Credential -AcceptKey

If all goes well, you'll be returned back to the console. If so, we can now use this session with a number of commands. For example, if I need to download a file from the SFTP server to my local computer, I can use the Get-SFTPFile function.

$getParams = @{
    SessionId = $session.SessionId
    LocalPath = 'C:localfile.txt'
   RemoteFile = 'C:localfile.txt'
}
Get-SFTPFile @getParams

Perhaps we need to remove a file on the SFTP server. We can use the Remove-SFTPItem function for that just about as easily as we use the Get-SFTPFile function.

Get-SFTPFile -SessionId $session.SessionId -Path 'C:localfile.txt'

Once we've done whatever we need to do on the SFTP server, we should then disconnect and remove the session. We could just call Remove-SFTPSession and provide the session but it's always better to check ahead of time just to be sure the session is still there. We can check to see if the session is still created by using the Get-SFTPSession.

Summary

You can see below that I'm first checking to see if our session exists. If so, I'm then disconnecting it and completely removing it from our session.

if ($session = Get-SFTPSession -SessionId $session.SessionId) {
    $session.Disconnect()
}
$null = Remove-SftpSession -SftpSession $session

The next time you need to perform any kind of SFTP task, have a look at the Posh-SSH PowerShell module.

Image Credit: kikujungboy/Shutterstock
Adam Bertram
business.com Contributing Writer
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.