Search for answers or browse our knowledge base.
Uploading and Retrieving Files Over FTP Using WinSCP Scripts for ECM Batch Imports
Introduction
WinSCP (Windows Secure Copy) is a widely used open-source SFTP, FTP, and SCP client for Windows. This knowledge base article provides PowerShell scripts for uploading and retrieving files over FTP using WinSCP. Additionally, it guides you on how to create a Windows Task Scheduler job to automate these scripts. The scripts are designed to run on PowerShell 7.3 and newer.
Prerequisites
Before using the provided scripts, ensure the following:
- WinSCP is installed on your system.
- PowerShell 7.3 or a newer version is installed.
FTP Server Upload Script Template
# Needed for PowerShell 7.3 and newer
# FTP Server Upload Script Template
$PSNativeCommandArgumentPassing = "Legacy"
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/command `
"open ftpes://USERNAME:PASSWORD@ftp.mydomain.com/ -rawsettings ProxyPort=0" `
"lcd c:\LOCAL DIRECTORY" `
"cd /REMOTE DIRECTORY" `
"put *" `
"exit"
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host "Success"
}
else
{
Write-Host "Error"
}
exit $winscpResult
FTP Server Download Script with Delete Template
# Needed for PowerShell 7.3 and newer
# FTP Server Download Script with Delete Template
$PSNativeCommandArgumentPassing = "Legacy"
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/command `
"open ftpes://USERNAME:PASSWORD@ftp.mydomain.com/ -rawsettings ProxyPort=0" `
"lcd c:\LOCAL DIRECTORY" `
"cd /REMOTE DIRECTORY" `
"get -delete *" `
"exit"
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host "Success"
}
else
{
Write-Host "Error"
}
exit $winscpResult
Creating a Windows Task Scheduler Job
Open Windows Task Scheduler:
- Press
Win + S
and type “Task Scheduler.” - Select “Task Scheduler” from the search results.
Create a Basic Task:
- In the right-hand Actions pane, click on “Create Basic Task.”
Provide a Name and Description:
- Enter a name and description for your task, then click “Next.”
Choose Trigger:
- Select the trigger that suits your automation needs (e.g., daily, weekly), then click “Next.”
Set the Start Date and Time:
- Configure the start date and time based on your preferences, then click “Next.”
Choose Action:
- Select “Start a program,” then click “Next.”
Configure the Program/Script:
- Browse and select the PowerShell executable (e.g.,
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
). - In the “Add arguments” field, enter the path to your script (e.g.,
C:\Path\To\Your\Script.ps1
). - Click “Next” and then “Finish.”
Test the Task:
- In Task Scheduler, find your created task in the center pane.
- Right-click and select “Run” to test the task’s execution.
WinSCP Commands
WinSCP also supports commands for more advanced scripting. Refer to the WinSCP Documentation for details on scripting. Here are some example commands:
- call: Executes arbitrary remote shell command
- cd: Changes remote working directory
- checksum: Calculates checksum of remote file
- chmod: Changes permissions of remote file
- close: Closes session
- cp: Duplicates remote file
- echo: Prints message onto script output
- exit: Closes all sessions and terminates the program
- get: Downloads file from remote directory to local directory
- help: Displays help
- keepuptodate: Continuously reflects changes in the local directory on the remote one
- lcd: Changes local working directory
- lls: Lists the contents of the local directory
- ln: Creates a remote symbolic link
- lpwd: Prints local working directory
- ls: Lists the contents of the remote directory
- mkdir: Creates a remote directory
- mv: Moves or renames a remote file
- open: Connects to the server
- option: Sets or shows the value of script options
- put: Uploads a file from the local directory to the remote directory
- pwd: Prints the remote working directory
- rm: Removes a remote file
- rmdir: Removes a remote directory
- session: Lists connected sessions or selects an active session
- stat: Retrieves attributes of a remote file
- synchronize: Synchronizes a remote directory with a local one
Notes:
Execution and Automation
- WinSCP Must be Installed for Script to Function
- Recommend Windows Task Scheduler Job for Automation of Script
File Transfer and Deletion
- Delete Rule can be removed by taking the -delete out of the get statement or added to the upload statement by adding -delete after put statement
Security and Configuration
- Site is secured by FTPS and requires a secured connection if using another method to obtain data from FTP server
- See WinSCP Documentation for more details on configurations and additional features like synchronizing, time stamps, and other optional settings
- ldc = local directory
- cd = change directory on the FTP server
- put = upload file to server
- get = download file to server
- get -delete = gets files then deletes files from FTP folder
- put -delete = uploads files then deletes files from the local folder
Additional Resources
- WinSCP Documentation: WinSCP Documentation
Feel free to customize the scripts according to your specific requirements and refer to the WinSCP documentation for more advanced configurations and features. Automating these scripts using the Windows Task Scheduler adds efficiency to your file transfer operations.