WinSCP and defined port WinSCP and defined port powershell powershell

WinSCP and defined port


WinSCP SessionOptions LINK

WinSCP Session Options default to SFTP and PORT 22 so a normal session object just needs a few things like this...

Note: I'm going to display in Visual Basic 6 for anyone having trouble using the library in this manner, but the logic is similar for VB.net, C#, and PowerShell as the question asks.

Dim sessionOptionsSet sessionOptions = CreateObject("WinSCP.SessionOptions")With sessionOptions .HostName = SftpUrl .UserName = UserName .Password = UserPassword .SshHostKeyFingerprint = "ssh-rsa 2048 Ue9.................................="End WithDim sftpSessionSet sftpSession = CreateObject("WinSCP.Session")On Error GoTo YOURERRORsftpSession.open sessionOptionsIf sftpSession.opened Then    'Do stuff...End If

The above code is working and connecting to a real server.

In your question, you originally asked for FTP, although you did correct and say SFTP. However, I'll also display a FTP request too as WinSCP supports it and the example WOULD need to set at least the protocol.

WinSCP sets the port based on the protocol used, so in the below example we still do not need to set the port.

One would only need to set the port if it differed from default server ports for the protocol used.

Dim sessionOptionsSet sessionOptions = CreateObject("WinSCP.SessionOptions")With sessionOptions .Protocol = Protocol_Ftp .HostName = FtpUrl .UserName = UserName .Password = UserPasswordEnd WithDim FtpSessionSet FtpSession = CreateObject("WinSCP.Session")On Error GoTo YOURERRORFtpSession.open sessionOptionsIf FtpSession.opened Then    'Do stuff...End If


It's hard to say without any code but try something like this:

$sessionOptions = New-Object WinSCP.SessionOptions$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp$sessionOptions.PortNumber = 2222$sessionOptions.HostName = "example.com"$sessionOptions.UserName = "user"$sessionOptions.Password = "mypassword"