Download large files from SFTP server using Get-SFTPItem powershell

venkatesh padmanabhan 181 Reputation points
2022-04-05T06:45:46.26+00:00

Hi.

I am trying to download large files from SFTP server using the powershell command

$mySession = New-SFTPSession -ComputerName 'abc' -Credential $Credential -ConnectionTimeout 10000 -AcceptKey

Get-SFTPItem -SFTPSession $mysession -path $_.FullName -Destination "path"

The above command is taking a longer time to download file, when the file size is huge .

How to fix this issue while downloading larger files ?
Thanks

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-04-05T15:13:15.573+00:00

    Have you increased the ConnectionTimeout value from 10000 to, say, 30000? Is it possible the connection is ended by the remote host?

    The Posh-SSH module is a third-party software. If you're unable to complete the transfer no matter how large you make the ConnectionTimeout value you should contact the author of the module on GitHub.

    0 comments No comments

  2. Limitless Technology 39,926 Reputation points
    2022-04-07T13:43:01.57+00:00

    Hi @venkatesh padmanabhan

    The overriding factor for slow downloads via SFTP is your Internet connection and the connection speed (and any limits) of the SFTP server's connection. There's no programmatic way to make the transfer faster. Here's a powershell script example you may like to customise and try:

    try
    {
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"

    # Setup session options  
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{  
        Protocol = [WinSCP.Protocol]::Sftp  
        HostName = "example.com"  
        UserName = "user"  
        Password = "mypassword"  
        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="  
    }  
     
    $session = New-Object WinSCP.Session  
     
    try  
    {  
        # Connect  
        $session.Open($sessionOptions)  
     
        # Download files  
        $transferOptions = New-Object WinSCP.TransferOptions  
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary  
     
        $remotePath = "/home/user/*";  
        $localPath = "d:\download\*";  
        $transferResult =  
            $session.GetFiles($remotePath, $localPath, $False, $transferOptions)  
     
        # Throw on any error  
        $transferResult.Check()  
     
        # Print results  
        foreach ($transfer in $transferResult.Transfers)  
        {  
            Write-Host "Download of $($transfer.FileName) succeeded"  
        }  
    }  
    finally  
    {  
        # Disconnect, clean up  
        $session.Dispose()  
    }  
     
    exit 0  
    

    }
    catch [Exception]
    {
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
    }

    You may want to consider a different transfer protocol.

    I hope this answers your question.

    Thanks.

    --
    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.