BDC Hamburger Icon

MENU

Close
BDC Logo
Search Icon
Advertising Disclosure
Close
Advertising Disclosure

Business.com aims to help business owners make informed decisions to support and grow their companies. We research and recommend products and services suitable for various business types, investing thousands of hours each year in this process.

As a business, we need to generate revenue to sustain our content. We have financial relationships with some companies we cover, earning commissions when readers purchase from our partners or share information about their needs. These relationships do not dictate our advice and recommendations. Our editorial team independently evaluates and recommends products and services based on their research and expertise. Learn more about our process and partners here.

Updated Aug 09, 2023

Managing Files Over SFTP With PowerShell

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

author image
Written By: Adam BertramSenior Writer
Verified CheckEditor Verified:
Verified Check
Editor Verified
Close
A business.com editor verified this analysis to ensure it meets our standards for accuracy, expertise and integrity.
Gretchen Grunburg
Senior Editor
Business.com earns commissions from some listed providers. Editorial Guidelines.
Table Of Contents Icon

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.

Did you find this content helpful?
Verified CheckThank you for your feedback!
author image
Written By: Adam BertramSenior Writer
Adam Bertram is an IT expert and business owner who has spent decades advising on network administration and security, designing and building infrastructure, and creating and teaching courses on Windows Server, Powershell and more. While maintaining his own IT business, he has provided hands-on DevsOps services for clients like JPMorgan Chase. At business.com, Adam covers the ins and outs of PowerShell, helping companies improve their Windows configurations and automations. Bertram, who has a degree in computer science, holds Microsoft, Cisco and CompTIA credentials. He has written numerous tutorials, guides and books, including "Building Better PowerShell Code: Applying Proven Practices One Tip at a Time."
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