You can use PowerShell and POSH-SSH to complete such a task (Most of script borrowed from https://community.spiceworks.com/topic/2233481-posh-ssh-downloading-from-an-sftp-server (made edits to it so do not use this one or you will potentially delete all your files on the server). Additionally, the script on this is outdated and some commands needed to be updated.)
1) Run PowerShell or PowerShell ISE as Administrator
2) Run the command Set-ExecutionPolicy -ExecutionPolicy Unrestricted
(make sure to restrict after you are done using the script).
3) Run the command install-module posh-ssh
4) Run the command import-module posh-ssh
5) Create an encrypted key for $encrypted
a) Type $secure = Read-Host -AsSecureString
and a window will appear in PowerShell
Type in your password in the window and hit OK
b) Run the command $encrypted = ConvertFrom-SecureString -SecureString $secure
c) Run the command $encrypted
and your string will appear.
d) Copy and paste the string into $encrypted
(in the script below) and ensure that it is on one line.
The following example for $encrypted
will work:
The following example for $encrypted
will not work:
e) Fill in $ComputerName
with your server's IP address.
Fill in $username
with the username on the sftp server.
Change $LocalPath
to a folder on your device.
Change $SftpPath
to the directory location on the sftp server.
f) Run the script below with all the steps above completed and enjoy downloading all of your files from a directory.
# Define Server Name
$ComputerName = "127.0.0.1"
# Define UserName
$username = "username"
$encrypted = "InsertEncryptedStringHere"
$password = ConvertTo-SecureString -String $encrypted
#Set Credetials to connect to server
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $username, $password
# Set local file path and SFTP path
$LocalPath = "C:\Users\username\Downloads\test\"
$SftpPath = '/var/www/html/'
# Establish the SFTP connection
$SFTPSession = New-SFTPSession -ComputerName $ComputerName -Credential $Credential -AcceptKey
# lists directory files into variable
$FilePath = Get-SFTPChildItem -sessionID $SFTPSession.SessionID -path $SftpPath
#For each file listed in the directory below copies the files to the local directory and then deletes them from the SFTP one at a time looped until all files
#have been copied and deleted
ForEach ($LocalFile in $FilePath)
{
#Ignores '.' (current directory) and '..' (parent directory) to only look at files within the current directory
if($LocalFile.name -eq "." -Or $LocalFile.name -eq ".." )
{
Write-Host "Files Ignored!"
}
else
{
Write-Host $LocalFile
get-sftpitem -SessionId $SFTPSession.SessionID -Path $localfile.fullname -Destination $LocalPath -Force
}
}
#Terminates the SFTP session on the server
Remove-SFTPSession -SessionId $SFTPSession.SessionID
Example
What's on my server (via WinSCP):
What was downloaded with the script (locally):
Get-SFTPItem (Documentation)
https://github.com/darkoperator/Posh-SSH/blob/master/docs/Get-SFTPItem.md
-------------------------------------------
If this is helpful please accept answer.