How to handle files with PowerShell over SSH
This article has not been completed yet. However, it may already contain helpful information and therefore it has been published at this stage.
Introduction:
Posh-SSH Module:
# Installing Posh Module
Write-Host "Installing the Posh-SSH Module" -ForegroundColor Green
install-module posh-ssh
# Show all available module commands
Write-Host "Listing all Posh-SSH - Commands..." -ForegroundColor Green
Get-Command -Module Posh-SSH
CommandType Name Version Source
----------- ---- ------- ------
Function Get-PoshSSHModVersion 2.3.0 Posh-SSH
Function Get-SFTPChildItem 2.3.0 Posh-SSH
Function Get-SFTPContent 2.3.0 Posh-SSH
Function Get-SFTPLocation 2.3.0 Posh-SSH
Function Get-SFTPPathAttribute 2.3.0 Posh-SSH
Function Get-SFTPSession 2.3.0 Posh-SSH
Function Get-SSHPortForward 2.3.0 Posh-SSH
Function Get-SSHSession 2.3.0 Posh-SSH
Function Get-SSHTrustedHost 2.3.0 Posh-SSH
Function Invoke-SSHCommand 2.3.0 Posh-SSH
Function Invoke-SSHCommandStream 2.3.0 Posh-SSH
Function Invoke-SSHStreamExpectAction 2.3.0 Posh-SSH
Function Invoke-SSHStreamExpectSecureAction 2.3.0 Posh-SSH
Function Invoke-SSHStreamShellCommand 2.3.0 Posh-SSH
Function Move-SFTPItem 2.3.0 Posh-SSH
Function New-SFTPFileStream 2.3.0 Posh-SSH
Function New-SFTPItem 2.3.0 Posh-SSH
Function New-SFTPSymlink 2.3.0 Posh-SSH
Function New-SSHDynamicPortForward 2.3.0 Posh-SSH
Function New-SSHLocalPortForward 2.3.0 Posh-SSH
Function New-SSHRemotePortForward 2.3.0 Posh-SSH
Function New-SSHShellStream 2.3.0 Posh-SSH
Function New-SSHTrustedHost 2.3.0 Posh-SSH
Function Remove-SFTPItem 2.3.0 Posh-SSH
Function Remove-SFTPSession 2.3.0 Posh-SSH
Function Remove-SSHSession 2.3.0 Posh-SSH
Function Remove-SSHTrustedHost 2.3.0 Posh-SSH
Function Rename-SFTPFile 2.3.0 Posh-SSH
Function Set-SFTPContent 2.3.0 Posh-SSH
Function Set-SFTPLocation 2.3.0 Posh-SSH
Function Set-SFTPPathAttribute 2.3.0 Posh-SSH
Function Start-SSHPortForward 2.3.0 Posh-SSH
Function Stop-SSHPortForward 2.3.0 Posh-SSH
Function Test-SFTPPath 2.3.0 Posh-SSH
Cmdlet Get-SCPFile 2.3.0 Posh-SSH
Cmdlet Get-SCPFolder 2.3.0 Posh-SSH
Cmdlet Get-SCPItem 2.3.0 Posh-SSH
Cmdlet Get-SFTPFile 2.3.0 Posh-SSH
Cmdlet Get-SFTPItem 2.3.0 Posh-SSH
Cmdlet New-SFTPSession 2.3.0 Posh-SSH
Cmdlet New-SSHSession 2.3.0 Posh-SSH
Cmdlet Set-SCPFile 2.3.0 Posh-SSH
Cmdlet Set-SCPFolder 2.3.0 Posh-SSH
Cmdlet Set-SCPItem 2.3.0 Posh-SSH
Cmdlet Set-SFTPFile 2.3.0 Posh-SSH
Cmdlet Set-SFTPFolder 2.3.0 Posh-SSH
Cmdlet Set-SFTPItem 2.3.0 Posh-SSH
# Connecting to the SFTP - Server
Write-Host "Connecting..." -ForegroundColor Green
New-SFTPSession -Computername 127.0.0.1
C:\SFTP\tom
# Creating a txt file on SFTP server
Write-Host "Creating file..." -ForegroundColor Green
New-SFTPItem -SessionId 0 -Path "/SFTP/tom/test2.txt" -ItemType file
# List subdirectory files on SFTP server
Write-Host "Listing files..." -ForegroundColor Green
Get-SFTPChildItem -SessionId 0 -Path "/SFTP/tom/"
# Download file to client
Write-Host "Download file to client" -ForegroundColor Green
Get-SFTPFile -SessionId 0 -RemoteFile /SFTP/tom/Test.txt -LocalPath c:\temp
# Upload file to SFTP server
Write-Host "Upload file to SFTP server" -ForegroundColor Green
Set-SFTPFile -SessionId 0 -LocalFile c:\temp\TestIII.txt -RemotePath /SFTP/tom -Overwrite:$true
WinSCP
Installing the Assembly
First, you need to install the WinSCP .NET assembly. In most cases, all you need to do is download the WinSCP-X.X.X-Automation.zip
package and extract it along with your PowerShell script.
cd c:\temp
# Install WinSCP Module
Install-Module -Name WinSCP
Get-Command -Module WinSCP
CommandType Name Version Source
----------- ---- ------- ------
Alias Close-WinSCPSession 5.17.10.0 WinSCP
Alias Enter-WinSCPSession 5.17.10.0 WinSCP
Alias Exit-WinSCPSession 5.17.10.0 WinSCP
Alias Open-WinSCPSession 5.17.10.0 WinSCP
Function ConvertTo-WinSCPEscapedString 5.17.10.0 WinSCP
Function Copy-WinSCPItem 5.17.10.0 WinSCP
Function Get-WinSCPChildItem 5.17.10.0 WinSCP
Function Get-WinSCPHostKeyFingerprint 5.17.10.0 WinSCP
Function Get-WinSCPItem 5.17.10.0 WinSCP
Function Get-WinSCPItemChecksum 5.17.10.0 WinSCP
Function Get-WinSCPSession 5.17.10.0 WinSCP
Function Invoke-WinSCPCommand 5.17.10.0 WinSCP
Function Move-WinSCPItem 5.17.10.0 WinSCP
Function New-WinSCPItem 5.17.10.0 WinSCP
Function New-WinSCPItemPermission 5.17.10.0 WinSCP
Function New-WinSCPSession 5.17.10.0 WinSCP
Function New-WinSCPSessionOption 5.17.10.0 WinSCP
Function New-WinSCPTransferOption 5.17.10.0 WinSCP
Function New-WinSCPTransferResumeSupport 5.17.10.0 WinSCP
Function Receive-WinSCPItem 5.17.10.0 WinSCP
Function Remove-WinSCPItem 5.17.10.0 WinSCP
Function Remove-WinSCPSession 5.17.10.0 WinSCP
Function Rename-WinSCPItem 5.17.10.0 WinSCP
Function Send-WinSCPItem 5.17.10.0 WinSCP
Function Start-WinSCPConsole 5.17.10.0 WinSCP
Function Sync-WinSCPPath 5.17.10.0 WinSCP
Function Test-WinSCPPath 5.17.10.0 WinSCP
# Set credentials to a PSCredential Object.
$credential = Get-Credential
$sshHostKeyFingerprint = "ssh-ed25519 255 5c:ff:36:de:0e:ae:97:b7:4d:5d:90:51:3c:c3:f9:b3"
$sessionOption = New-WinSCPSessionOption -HostName localhost -SshHostKeyFingerprint $sshHostKeyFingerprint -Credential $credential
$session = New-WinSCPSession -SessionOption $sessionOption
# Using the WinSCPSession, download the file from the remote host to the local host.
Receive-WinSCPItem -WinSCPSession $session -Path "/SFTP/tom/TestIII.txt" -Destination "C:\temp\"
Get-WinSCPChildItem -WinSCPSession $session -Path "/SFTP/tom/"
Send-WinSCPItem -WinSCPSession $session -Path 'C:\temp\TestIII.txt' -Destination '/SFTP/tom/Test4.txt'
New-WinSCPItem -WinSCPSession $session -Path '/SFTP/tom/' -Name 'TextFile.txt' -ItemType File -Value 'Hello World!'
Close-WinSCPSession -WinSCPSession $session