Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Created June 28, 2021 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/fcc9568fa00647b1f383089279ae411e to your computer and use it in GitHub Desktop.
Save joerodgers/fcc9568fa00647b1f383089279ae411e to your computer and use it in GitHub Desktop.
Example of how to download a file using Posh-SSH via SSH/SFTP
#requires -Modules Posh-SSH
$hostname = "server.contoso.com"
$keyFile = "C:\_temp\ssh-key.pem"
$userName = "johndoe"
$certPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force
$localPath = "c:\_temp\file.dat"
$remotePath = "/folder1/folder2/folder3"
$remoteFile = "file.dat"
New-SFTPSession `
-ComputerName $hostname `
-KeyFile $keyFile `
-Credential ([PSCredential]::new($userName, $certPassword)) `
-AcceptKey
Set-SFTPLocation `
-SessionId 0 `
-Path $remotePath
Get-SFTPFile `
-SessionId 0 `
-RemoteFile $remoteFile `
-LocalPath $localPath `
-Overwrite
Remove-SFTPSession `
-SessionId 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment