MENU
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.
Native support for Secure File Transfer Protocol (SFTP) is not in PowerShell.
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.
You can retrieve, delete and add new files by using a free module called Posh-SSH, despite PowerShell’s non-native support for SFTP.
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.
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
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’
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.
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.