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

A dragon IT Architect in the shadowrun world using an Excel document and the Azure Quick Review Tool

Azure Quick Review 2.04 – High Level Assessments

With Azure Quick Review (azqr) you can quickly make a High Level Assessment of an “Azure Subscription” or “Resource Group”. Installation of Azure Quick Review You can download the files…

Spread the knowledge
Read more
A dragon IT Architect in the shadowrun world using Microsoft Tools like Windows LAPS to secure his datacenter, relaxing and watching a sitcom on TV

Securing Local Administrator Accounts with Windows LAPS: A Get Started Guide for Windows Server Environments

Managing local administrator accounts securely is a critical aspect of maintaining a robust and secure IT environment. With the introduction of the Windows Local Administrator Password Solution (LAPS) in newer…

Spread the knowledge
Read more
A dragon IT Architect in the shadowrun world using his notebook and keepassxc to safe the world from a virus attack

Securing Client Passwords with KeePassXC: A Consultant’s Guide to Autotyping and More

As consultants, managing multiple clients and their respective passwords can be a daunting task, especially when security is paramount. In this article, we will explore how KeePassXC, a powerful and…

Spread the knowledge
Read more
Windows Server 2025 Monitoring TCP/IP on http://hartiga.de

Windows Server 2025 – Monitoring TCP/IP

Monitoring open TCP/IP ports is crucial for maintaining the security and functionality of your Windows system. This article will guide you through using three powerful tools, netstat, powershell Get-NetTCPConnection and…

Spread the knowledge
Read more
Windows Admin using Winget

Easily manage Applications with WinGet v1.x

Microsoft has released WinGet, a package manager that is very well suited for installing and updating third-party applications. We are now installing applications with “WinGet” and learn how to manage…

Spread the knowledge
Read more
hyper-v manager and a Linux Terminal

Squid on Hyper-V – new Ubuntu 24.04

A Linux VM with Proxy, such as Squid, can be used to reduce bandwidth as it caches frequently requested websites. Today we want to configure a Linux VM with Proxy and deploy…

Spread the knowledge
Read more