Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Upload files to an SFTP server via PowerShell
#Upload files to an SFTP server via PowerShell
#Requires WinSCP to be installed on local machine
# Load WinSCP .NET assembly
Add-Type -Path "C:\path\to\WinSCPnet.dll"
# Password prompt (not obscured)
$title = 'Credentials'
$msg = 'Enter your password:'
$pass = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
# Display name of server
$servername = "FTP Server Descriptive Name"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = ""
UserName = "username"
Password = $pass
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$sessionOptions.AddRawSettings("AuthGSSAPI", "1")
$sessionOptions.AddRawSettings("TcpNoDelay", "1")
$sessionOptions.AddRawSettings("FSProtocol", "2")
$session = New-Object WinSCP.Session
# Connect
Write-Host -ForegroundColor Cyan "`r`nUploading files to $servername..."
Write-Host -ForegroundColor White "Do not close this window!"
# Create folder
$date = $((Get-Date).ToString('yyyyMMdd'))
$path = "/path/to/target/"+($date)+"/"
# Transfer files
$session.PutFiles("c:\path\to\files\*.*", $path+"/*").Check()
#Inform user
[System.Windows.Forms.MessageBox]::Show("Files have been uploaded to $servername.")
#Dispose of sesssion
Copy link

mhabib1981 commented Nov 7, 2019

Hi thanks for the script.
Can you please write for SFTP as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment