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:
- Open Settings.
- Go to Apps > Optional features.
- Search for OpenSSH Client.
- 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.
- Download PuTTY from putty.org.
- Install it to the default path:
C:\Program Files\PuTTY\. - Confirm
psftp.exeexists 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
- Checks that the local file exists before connecting.
- Creates a temporary command file with the SFTP instructions.
- Passes the command file to
sftpusing the-bflag. - Deletes the temporary command file after the upload.[!NOTE]
The Windows built-insftpclient 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
- Validates that psftp.exe and the local file both exist.
- Creates a psftp command file including the password using
-pw. - Runs psftp silently and appends all output to a log file.
- Reports success or failure based on the exit code.[!IMPORTANT]
The-pwflag 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?
| Scenario | Recommended script |
|---|---|
| Quick upload, no extra software | Option 1 โ OpenSSH |
| Automated upload with password | Option 2 โ psftp single file |
| Upload entire folder automatically | Option 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:
- Open Task Scheduler from the Start menu.
- Select Create Basic Task.
- Enter a name and description.
- Set a trigger โ daily, weekly, or at a specific time.
- Select Start a program as the action.
- Browse to your
.batfile. - Select Finish.[!TIP]
For scheduled tasks, remove thepausecommand 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:
- Open Command Prompt.
- Run
ssh-keygenand follow the prompts. Press Enter to accept the default path. - This creates two files:
id_rsa(private key) andid_rsa.pub(public key). - Copy the contents of
id_rsa.pubto the serverโs~/.ssh/authorized_keysfile. - Remove the
SET SFTP_PASSline from your batch script. - 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
| Issue | Fix |
|---|
| Issue | Fix |
|---|---|
sftp command not found | Install OpenSSH Client in Settings > Optional features |
psftp.exe not found | Install PuTTY from putty.org and confirm the install path |
| Permission denied | Check username, password, or SSH key configuration |
| File not found error | Confirm the LOCAL_FILE or LOCAL_FOLDER path is correct |
| Upload fails silently | Check the log file for the full error message |
| Connection timeout | Confirm port 22 is open on the server firewall |

