How to automate SFTP file uploads using a Windows batch script


Applies to: Windows 10 | Windows 11 | OpenSSH | PuTTY | Task Scheduler

Manually uploading files to an SFTP server every day is time-consuming. A Windows batch script automates the process so uploads run on demand or on a schedule without any manual steps. This article walks you through three batch script options depending on your environment and requirements.


In this article

  • Prerequisites
  • Option 1: Single file upload using Windows OpenSSH
  • Option 2: Single file upload using PuTTY psftp
  • Option 3: Bulk folder upload using PuTTY psftp
  • Schedule uploads with Task Scheduler
  • Switch to SSH key authentication
  • Troubleshooting

Prerequisites

Before you start, confirm the following:

  • You are running Windows 10 or Windows 11.
  • You have a valid username, password, and server address for the SFTP server.
  • You know the remote folder path on the server.

Option A โ€” Windows built-in OpenSSH

No extra software needed. OpenSSH is included in Windows 10 and Windows 11.

To confirm it is installed:

  1. Open Settings.
  2. Go to Apps > Optional features.
  3. Search for OpenSSH Client.
  4. If it is not listed as installed, select Add a feature and install it.

Option B โ€” PuTTY psftp

Required for scripts that pass the password inline without a manual prompt.

  1. Download PuTTY from putty.org.
  2. Install it to the default path: C:\Program Files\PuTTY\.
  3. Confirm psftp.exe exists at that path.

Option 1: Single file upload using Windows OpenSSH

Use this script when you do not want to install any extra software. It uses the built-in sftp command. You will be prompted to enter your password when the script runs.

text@echo off
REM ============================================================
REM  SFTP Upload Script - Windows OpenSSH
REM ============================================================

SET SFTP_HOST=sftp.xyz.com
SET SFTP_PORT=22
SET SFTP_USER=your_username
SET LOCAL_FILE=C:\Users\%USERNAME%\Desktop\report.pdf
SET REMOTE_DIR=/uploads

REM ---- CHECK IF FILE EXISTS ----
IF NOT EXIST "%LOCAL_FILE%" (
    echo [ERROR] File not found: %LOCAL_FILE%
    pause
    exit /b 1
)

REM ---- CREATE SFTP COMMAND FILE ----
echo lcd %~dp0> "%TEMP%\sftp_commands.txt"
echo cd %REMOTE_DIR%>> "%TEMP%\sftp_commands.txt"
echo put "%LOCAL_FILE%">> "%TEMP%\sftp_commands.txt"
echo ls -lh>> "%TEMP%\sftp_commands.txt"
echo bye>> "%TEMP%\sftp_commands.txt"

REM ---- CONNECT AND UPLOAD ----
echo [INFO] Connecting to %SFTP_HOST%...
sftp -P %SFTP_PORT% -b "%TEMP%\sftp_commands.txt" %SFTP_USER%@%SFTP_HOST%

IF %ERRORLEVEL% EQU 0 (
    echo [SUCCESS] File uploaded successfully.
) ELSE (
    echo [ERROR] Upload failed.
)

DEL "%TEMP%\sftp_commands.txt" 2>nul
pause
exit /b 0

How it works

  1. Checks that the local file exists before connecting.
  2. Creates a temporary command file with the SFTP instructions.
  3. Passes the command file to sftp using the -b flag.
  4. Deletes the temporary command file after the upload.[!NOTE]
    The Windows built-in sftp client does not support inline passwords in the command file. You will be prompted to enter your password manually each time this script runs. For fully automated uploads, use Option 2 or Option 3.

Option 2: Single file upload using PuTTY psftp

Use this script when you need fully automated uploads with an inline password. It uses PuTTYโ€™s psftp.exe and logs every session to a text file.

text@echo off
REM ============================================================
REM  SFTP Upload Script - PuTTY psftp
REM ============================================================

SET SFTP_HOST=sftp.xyz.com
SET SFTP_PORT=22
SET SFTP_USER=your_username
SET SFTP_PASS=your_password
SET LOCAL_FILE=C:\Users\%USERNAME%\Desktop\report.pdf
SET REMOTE_DIR=/uploads
SET PSFTP_PATH="C:\Program Files\PuTTY\psftp.exe"
SET LOG_FILE=%~dp0sftp-upload-log.txt

REM ---- VALIDATION ----
IF NOT EXIST %PSFTP_PATH% (
    echo [ERROR] psftp.exe not found at %PSFTP_PATH%
    pause
    exit /b 1
)

IF NOT EXIST "%LOCAL_FILE%" (
    echo [ERROR] File not found: %LOCAL_FILE%
    pause
    exit /b 1
)

REM ---- CREATE PSFTP COMMAND FILE ----
(
echo option batch on
echo option confirm off
echo open %SFTP_USER%@%SFTP_HOST% -pw %SFTP_PASS% -P %SFTP_PORT%
echo cd %REMOTE_DIR%
echo put "%LOCAL_FILE%"
echo ls
echo close
echo quit
) > "%TEMP%\psftp_commands.txt"

REM ---- RUN PSFTP ----
echo [INFO] Connecting to %SFTP_HOST%...
%PSFTP_PATH% -b "%TEMP%\psftp_commands.txt" >> "%LOG_FILE%" 2>&1

IF %ERRORLEVEL% EQU 0 (
    echo [SUCCESS] File uploaded. Log: %LOG_FILE%
) ELSE (
    echo [ERROR] Upload failed. Log: %LOG_FILE%
)

DEL "%TEMP%\psftp_commands.txt" 2>nul
pause
exit /b 0

How it works

  1. Validates that psftp.exe and the local file both exist.
  2. Creates a psftp command file including the password using -pw.
  3. Runs psftp silently and appends all output to a log file.
  4. Reports success or failure based on the exit code.[!IMPORTANT]
    The -pw flag passes the password in plain text. This is convenient but not secure for shared or public environments. See Switch to SSH key authentication for the recommended approach.

Option 3: Bulk folder upload using PuTTY psftp

Use this script to upload all files from a local folder to the remote server in one operation. It uses mput * to upload every file in the specified folder.

text@echo off
REM ============================================================
REM  SFTP Bulk Upload Script - PuTTY psftp
REM ============================================================

SET SFTP_HOST=sftp.xyz.com
SET SFTP_PORT=22
SET SFTP_USER=your_username
SET SFTP_PASS=your_password
SET LOCAL_FOLDER=C:\Users\%USERNAME%\Desktop\uploads
SET REMOTE_DIR=/uploads
SET PSFTP_PATH="C:\Program Files\PuTTY\psftp.exe"
SET LOG_FILE=%~dp0sftp-bulk-log.txt

REM ---- VALIDATION ----
IF NOT EXIST %PSFTP_PATH% (
    echo [ERROR] psftp.exe not found.
    pause
    exit /b 1
)

IF NOT EXIST "%LOCAL_FOLDER%" (
    echo [ERROR] Folder not found: %LOCAL_FOLDER%
    pause
    exit /b 1
)

REM ---- CREATE PSFTP COMMAND FILE ----
(
echo option batch on
echo option confirm off
echo open %SFTP_USER%@%SFTP_HOST% -pw %SFTP_PASS% -P %SFTP_PORT%
echo cd %REMOTE_DIR%
echo lcd "%LOCAL_FOLDER%"
echo mput *
echo ls -lh
echo close
echo quit
) > "%TEMP%\psftp_bulk.txt"

REM ---- RUN PSFTP ----
echo [INFO] Uploading all files from %LOCAL_FOLDER%...
%PSFTP_PATH% -b "%TEMP%\psftp_bulk.txt" >> "%LOG_FILE%" 2>&1

IF %ERRORLEVEL% EQU 0 (
    echo [SUCCESS] All files uploaded. Log: %LOG_FILE%
) ELSE (
    echo [ERROR] Upload failed. Log: %LOG_FILE%
)

DEL "%TEMP%\psftp_bulk.txt" 2>nul
pause
exit /b 0

Key difference from Option 2

The mput * command uploads every file in the LOCAL_FOLDER directory. The lcd command sets the local working directory inside the psftp session before the upload runs.


Which option should you use?

ScenarioRecommended script
Quick upload, no extra softwareOption 1 โ€” OpenSSH
Automated upload with passwordOption 2 โ€” psftp single file
Upload entire folder automaticallyOption 3 โ€” psftp bulk
Scheduled task (no manual input)Option 2 or 3 with SSH keys

Schedule uploads with Task Scheduler

To run an upload script automatically on a schedule:

  1. Open Task Scheduler from the Start menu.
  2. Select Create Basic Task.
  3. Enter a name and description.
  4. Set a trigger โ€” daily, weekly, or at a specific time.
  5. Select Start a program as the action.
  6. Browse to your .bat file.
  7. Select Finish.[!TIP]
    For scheduled tasks, remove the pause command at the end of the script so the window closes automatically after the upload finishes.

Switch to SSH key authentication

Storing passwords in plain text batch files is a security risk. SSH key authentication removes the need for a password entirely.

To set up SSH key authentication:

  1. Open Command Prompt.
  2. Run ssh-keygen and follow the prompts. Press Enter to accept the default path.
  3. This creates two files: id_rsa (private key) and id_rsa.pub (public key).
  4. Copy the contents of id_rsa.pub to the serverโ€™s ~/.ssh/authorized_keys file.
  5. Remove the SET SFTP_PASS line from your batch script.
  6. For psftp scripts, replace -pw %SFTP_PASS% with -i "C:\Users\%USERNAME%\.ssh\id_rsa".[!IMPORTANT]
    Never share your private key (id_rsa). Keep it on your local machine only. The public key (id_rsa.pub) is the one you put on the server.

Troubleshooting

IssueFix
IssueFix
sftp command not foundInstall OpenSSH Client in Settings > Optional features
psftp.exe not foundInstall PuTTY from putty.org and confirm the install path
Permission deniedCheck username, password, or SSH key configuration
File not found errorConfirm the LOCAL_FILE or LOCAL_FOLDER path is correct
Upload fails silentlyCheck the log file for the full error message
Connection timeoutConfirm port 22 is open on the server firewall


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top