Tuesday, January 21, 2025
HomeProgrammingUpload file to SFTP using PowerShell

Upload file to SFTP using PowerShell

To upload a file to an SFTP server using PowerShell, you can use the PSSFTP module or WinSCP with its automation API. Here’s a basic example using WinSCP:

  1. Download and install WinSCP from its official site.
  2. Load the WinSCP .NET assembly in PowerShell:
See also  Passing a JavaScript Function as a Parameter

powershell

Copy code

Add-Type -Path “C:\Program Files (x86)\WinSCP\WinSCPnet.dll”

  1. Set up the session and upload the file:

powershell

$session = New-Object WinSCP.Session

$sessionOptions = New-Object WinSCP.SessionOptions

$sessionOptions.HostName = “sftp.server.com”

$sessionOptions.UserName = “username”

$sessionOptions.Password = “password”

$sessionOptions.SshHostKeyFingerprint = “ssh-rsa 2048 …”

$session.Open($sessionOptions)

$session.PutFiles(“C:\path\to\local\file.txt”, “/remote/path/”).Check()

$session.Dispose()

This script authenticates to the SFTP server, uploads the file, and then closes the session. Make sure to replace the placeholders with your actual details.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x