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.

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

Related Posts

the IT Operations dragon looking at multiple screens with Group Policies on them and the GPO Policy Analyzer checking them all

How to use the GPO Policy Analyzer

Managing Group Policy Objects across a complex Active Directory environment has always been a challenge. Administrators often inherit hundreds of GPOs with overlapping configurations, stale settings, and hidden conflicts. Finding…

Read more
WAU Happy Dragons crazy about 110 percent security

Schedule Winget Auto Updates for operational usage

Deploying Winget Auto Updates (WAU) gives you a functional update baseline, for full functionality we need to configure more to get ready for production. If servers query the Winget repository…

Read more
WAU Happy Dragons about improved security

Centralized Software Patch Management: Deploying Winget Auto Updates (WAU) via Active Directory GPO

Deploying Winget Auto Updates for Software Patch Management for managing third-party software updates across an IT infrastructure typically requires expensive enterprise solutions. This article provides a technical guide on how…

Read more
Gemini Generated Image 6s2cz6s2cz6s2cz6

Active Directory Planning Tool: Mapping Structures and Delegations with SMAD-X

Using an Active Directory Planning Tool is key for understanding complex Active Directory environments and often requires more than what traditional management consoles can provide. While tools such as Active…

Read more
Dragons looking at AccountLockout Tool

AD Account Lockout (Free Tool)

The AD Account Lockout tool is free and very valuable in troubleshooting account lockouts in Active Directory. This is a task as old as the directory service itself. Even in…

Read more
IT Architect doing Active Directory Visualization

Active Directory Visualization (Free Tools)

Designing and documenting Active Directory (AD) structures is always a long and complex exercise. Historically, this required either building full lab environments or settling for static Visio diagrams that became…

Read more