business.com receives compensation from some of the companies listed on this page. Advertising Disclosure
World's Best Boss

Do you have the world's best boss?Enter them to win two tickets to Sandals!

BDC Hamburger Icon

MENU

Close
BDC Logo
Search Icon
Updated Aug 09, 2023

Managing Files Over SFTP With PowerShell

author image
Adam Bertram, Contributing Writer

Table of Contents

Open row

Secure File Transfer Protocol (SFTP) is a safe way of transferring files between hosts over the internet. While PowerShell does not offer native support for SFTP, you can add the functionality by using the free Posh-SSH module. In fact, there is a wide range of PowerShell modules that allow you to add extra functionalities for different tasks.

In this article, you’ll learn how to manage files over SFTP using PowerShell and the benefits of this approach.

How to manage files over SFTP with PowerShell

You can retrieve, delete and add new files by using a free module called Posh-SSH, despite PowerShell’s non-native support for SFTP. 

How to install Posh-SSH

Posh-SSH is available on the PowerShell Gallery. You can install it by running Install-Module -Name Posh-SSH. Once installed, these are the commands you get access to:

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

Let’s dig into some of the SFTP commands and see how they can be used.

TipBottom line

Check regularly that you’re running the latest version of the Posh-SSH module. New versions are frequently released featuring bug fixes and extra functionality. If you need to manage file permissions, learn how you can manage file system ACLs with PowerShell.

Setting up the Posh-SSH module

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

Instead, create a single SFTP session using the New-SFTPSession command. Take the easy route by not messing with the certificates. This means that you can use a username and password to authenticate to the SFTP server. The New-SFTPSession has a Credential parameter that accepts a PSCredential object. Use the Get-Credential command to prompt the user for a username and password.

$credential = Get-Credential

Once you have the username and password capture, 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

Did You Know?Did you know

The Posh-SSH module allows you to carry out many different SFTP operations, like adding new files and deleting and retrieving existing ones. PowerShell’s versatility doesn’t stop here. It’s also possible to manage IIS application pools and sync folders with PowerShell.

Working with files

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

$getParams = @{

    SessionId = $session.SessionId

    LocalPath = ‘C:localfile.txt’

   RemoteFile = ‘C:localfile.txt’

}

Get-SFTPFile @getParams

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

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

Bottom LineBottom line

Be mindful when downloading and removing files. Double-check your file paths to ensure data integrity.

Ending the session

Once you’ve done whatever you need to do on the SFTP server, you should then disconnect and remove the session. You 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. You can check to see if the session is still created by using the Get-SFTPSession.

First, check to see if your session exists. If it does, disconnect it and completely remove it from your 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.

The benefits of using PowerShell to manage files over SFTP

For managing files over SFTP in Windows-based systems, PowerShell is adaptable and versatile. You can use PowerShell to perform a variety of tasks, ranging from creating a web scraping tool to building an interactive menu inside a PowerShell script

Integrating your SFTP into PowerShell allows you to script and automate simple tasks, reducing the likelihood of manual errors and saving you time. You can also use PowerShell to help you manage user profiles effectively.

Where PowerShell really shines is its ability to handle complex tasks in easy-to-read code. This makes it a more accessible system if you and your team are at different programming skill levels. [Read related article: Why Speaking in Jargon Doesn’t Make You Look Smarter]

The versatility of the Posh-SSH module also makes your file management more efficient and effective thanks to its ability to manage a wide range of SFTP operations.

Mark Fairlie contributed to the writing and reporting in this article.

author image
Adam Bertram, 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.
BDC Logo

Get Weekly 5-Minute Business Advice

B. newsletter is your digest of bite-sized news, thought & brand leadership, and entertainment. All in one email.

Back to top