Run Powershell SFTP

Hi there, in this Powershell code i am going to use on “posh-ssh” module in order to work with Powershell for SFTP actions.

The module is not large repository but it contains useful commands that definitely will assist you to perform simple actions like, copy, remove, sessions and the following commands:

image
image

The following code will copy FTP file to local location, including encrypted password file which located on your computer.


#Import-Module:
install-module posh-ssh


#Export to CLIXML - run once!!
Get-Credential | Export-CliXml  -Path "C:\Users\PelegIT\Desktop\cred.xml"

#Credential: ############
$User = "root"
$File = "C:\Users\PelegIT\Desktop\CV\cred.xml"
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential `
 -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($credential)
###############################

#FTP Local File:
$local = "File name"
#Dest file (Local):
$Dest  = "C:\Windows\Temp\123\"
#Connect to SFTP:
$Session = New-SFTPSession -Computername 'FTPURL' -credential $MyCredential

#Copy the File:
Get-SFTPFile  -SessionId $Session.SessionID   -RemoteFile $local -LocalPath $Dest 
About Meir Peleg 337 Articles
My name is Meir Peleg, (Born 30/04/1991) live in ISRAEL. Live and breath IT world. Microsoft MVP Windows IT & VMware vExpert.