Use SFTP with Powershell

If you want to use SFTP with Powershell to transfer files, you can do this with the Posh-SSH module.

Installation of SFTP with Powershell

To retrieve or copy data via Powershell using SFTP, you can use the Posh-SSH module. Installation is relatively simple on a standard Windows 11. Simply install the module with Install-Module and confirm the source as trustworthy.

Install-Module -Name Posh-SSH
SFTP with Powershell Installation
Install-Module -Name Posh-SSH

If the following error message appears in older versions of Powershell and Windows, you might need to activate TLS 1.2 This is usually necessary in older environments.

Simply enter the following command in Powershell and try to install the module again.

#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module -Name Posh-SSH

Connection setup

The connection is established with New-SFTPLocation and saved in a session variable. The credentials are queried in the process. However, they can also be saved to a file as described in the article Powershell – Passwort in einer Datei Abspeichern and used again here.

$SFTPSession = New-SFTPSession -ComputerName <IP oder DNS Name> -Credential (Get-Credential)

In our homelab environment we will connect to linux1.ad.hartiga.de

$SFTPSession = New-SFTPSession -ComputerName linux1.ad.hartiga.de -Credential (Get-Credential)
New-SFTPSession to linux1.ad.hartiga.de
New-SFTPSession to linux1.ad.hartiga.de

Commands

The connection should then be open. You can now use the following command via the session variable $SFTPSession.

  • Get-SFTPLocation – Retrieve the current directory on the SFTP server
Get-SFTPLocation -SessionId $SFTPSession.SessionId

The upload and download is pretty much identical. The overwriting of existing files must be confirmed with the -Force parameter.

  • Set-SFTPItem – Transferring a file to the SFTP server
  • Get-SFTPItem – Download a file
#example to transfer files
Set-SFTPItem -SessionId $SFTPSession.SessionId -Path C:\Users\ahart\AppData\Local\Temp\test.txt -Destination /home/ah

#example to transfer files and force overwriting of existing file
Set-SFTPItem -SessionId $SFTPSession.SessionId -Path C:\Users\ahart\AppData\Local\Temp\test.txt -Destination /home/ah -Force

#example to get files
Get-SFTPItem -SessionId $SFTPSession.SessionId -Path /home/ah/test.txt -Destination C:\Users\ahart\AppData\Local\Temp

#example to transfer files and force overwriting of existing local file
Get-SFTPItem -SessionId $SFTPSession.SessionId -Path /home/ah/test.txt -Destination C:\Users\ahart\AppData\Local\Temp -Force
Example using Powershell and Posh-SSH
Example using Powershell and Posh-SSH

All commands can be found in the PowerShell Gallery from Posh-SSH.

If you want to learn more, please check out the video below.

Disconnect

At the end, you should disconnect your connection. This is done simply by using the Remove-SFTPSession command and specifying the session variable. A simple True should appear in response.

Remove-SFTPSession -SessionId $SFTPSession.SessionId
disconnect your connection
disconnect your connection

Find more Powershell commands here and the original version in German by Jörg Karsten is here.

Spread the knowledge
Avatar for Andreas Hartig
Andreas Hartig - MVP - Cloud and Datacenter Management, Microsoft Azure

Related Posts

IT Operations Drache looking at Event Logs

Modern Windows Event Viewer – EventLogExpert

The project Windows Event Viewer – EventLogExpert provides a modern open-source toolset that fundamentally improves the way we interact with Windows Event Logs. Since troubleshooting .evtx files is often a…

Spread the knowledge
Read more
IT Developer Dragon red with bronze skin

Windows Terminal and Oh My Posh – personalization for improved coding

Windows Terminal and Oh My Posh are a great combination! In the past we looked here at how to utilize the Windows Terminal in a personalized way to improve usability…

Spread the knowledge
Read more
Winget and IaC SystemEngineerDragon

WinGet and IaC – Take Winget to the next level

WinGet and IaC are maybe your next step to automate your environment. In the past, managing third-party applications on Windows meant 3rd party tools or gathering MSI installers on network…

Spread the knowledge
Read more
Dragon IT Operations logfile tailing using Klogg

Tail for log files with Windows – 2026 Edition

I view a lot of log files—probably more than I’d like to admit. Whether it’s troubleshooting a hybrid infrastructure issue or debugging a cloud deployment, I always look for the…

Spread the knowledge
Read more
dragon it system engineer grc benchmark

Windows DNS Performance Testing

DNS issues don’t always show up as clear outages. Often they show up as annoying browser behaviour like “random delays on first page load”, “sometimes it works, sometimes it spins”,…

Spread the knowledge
Read more
IT Security Dragon reading Windows Event Logs

Windows Server Event Log and Event Log Policies

Windows Server Event Log for most teams are only used when something already smells like incident:💥 DC misbehaving,💥 file server “mysteriously slow”,💥 SOC asking for “all the logs you have…

Spread the knowledge
Read more