Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Hi Martin,

I have already done it.

Re: Automate FTP download - Using WinSCP .NET Assembly

You need to provide actual values for the HostName, UserName, Password and the SshHostKeyFingerprint.

You can generate a code template with real values in GUI:

Automate FTP download - Using WinSCP .NET Assembly

I am using below powershell code to automate downloads from a remote FTP server.

param (

    $localPath = "c:\downloaded\",
    $remotePath = "/home/user/"
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = ""
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    $session = New-Object WinSCP.Session
        # Connect
        # Get list of files in the directory
        $directoryInfo = $session.ListDirectory($remotePath)
        # Select the most recent file
        $latest =
            $directoryInfo.Files |
            Where-Object { -Not $_.IsDirectory } |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1
        # Any file at all?
        if ($latest -eq $Null)
            Write-Host "No file found"
            exit 1
        # Download the selected file
        $session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check()
        # Disconnect, clean up
    exit 0
catch [Exception]
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1

I am getting below error when executed using powershell.

Error: The value supplied is not valid, or the property is read-only. Change the value, and then try again

Please help as i am new to powershell scripting!!