Skip to content
linscpSecure file transfer for Linux and Windows
GitHub

Automation & scripting

WinSCP .NET assembly and PowerShell

Drive linscp-cli from C# or PowerShell with WinSCP's .NET assembly, on Windows and on Linux.

Reviewed

When a job needs loops, conditions or real error handling, a script language plus WinSCP’s .NET assembly (WinSCPnet.dll) is often easier than a linscp-cli script. The assembly does not transfer files itself. It starts a console client in the background, sends it commands and reads the XML log back as objects. linscp-cli speaks the protocol the assembly expects, so it can take the place of WinSCP’s executable.

This page explains how to connect the two. For the classes and methods (Session, SessionOptions, TransferOptions and the rest), use WinSCP’s own .NET assembly reference.

The linscp packages for Windows include a dotnet folder in the installation directory. It holds WinSCPnet.dll, built for .NET Framework 4 from the same WinSCP source as linscp, and a copy of linscp-cli named WinSCP.exe. The assembly looks for WinSCP.exe next to itself, so code that loads it from that folder works without further setup.

The assembly that WinSCP itself publishes also works with linscp-cli on Windows, provided its version matches the WinSCP version linscp is built from. The assembly checks this when it starts.

Set two properties on the Session before you open it:

  • ExecutablePath: the full path of linscp-cli (on Windows, the WinSCP.exe copy in the dotnet folder works too).
  • DisableVersionCheck = true: on Linux only. The assembly tries to read the version from a Windows resource that a Linux executable does not have.
using WinSCP;
var options = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "files.example.org",
UserName = "deploy",
SshHostKeyFingerprint = "ssh-ed25519 255 Jh3k...Qw8",
SshPrivateKeyPath = "/home/alice/.ssh/deploy.ppk",
};
using var session = new Session
{
ExecutablePath = "/usr/bin/linscp-cli",
DisableVersionCheck = !OperatingSystem.IsWindows(),
};
session.Open(options);
session.PutFiles("/home/alice/site/public/*", "/var/www/html/").Check();

The same in PowerShell 7:

Terminal window
Add-Type -Path /home/alice/winscpnet/WinSCPnet.dll
$session = New-Object WinSCP.Session
$session.ExecutablePath = "/usr/bin/linscp-cli"
$session.DisableVersionCheck = $true
# ... SessionOptions and $session.Open(...) as in WinSCP's documentation
  • SFTP, FTP, FTPS, WebDAV and S3 sessions work through the assembly on both systems; linscp’s test suite covers each of them.
  • By default the assembly starts linscp-cli with an empty configuration (/ini=nul), so stored sites and accepted host keys are not used. Give the host key in SessionOptions.
  • The COM interface of the assembly (for VBScript, JScript or VBA) is not part of linscp.
  • WinSCP’s PowerShell extensions in Preferences › Commands run on Windows only, where Windows PowerShell and the bundled assembly are present.