Automate File Backups with Batch Script in Windows 11
Automating file backups in Windows 11 ensures your critical data is
consistently protected without requiring manual intervention. By writing
a simple batch script using the native robocopy command and
scheduling it with the Windows Task Scheduler, you can automatically
mirror files and folders to an external drive, network share, or
secondary storage location on a set schedule.
Step 1: Create the Backup Batch Script
- Open Notepad (or any text editor).
- Paste the following script into the document:
@echo off
:: Define source and backup directories
set "SOURCE=C:\Users\YourUsername\Documents"
set "DESTINATION=D:\Backups\Documents"
:: Create destination folder if it doesn't exist
if not exist "%DESTINATION%" mkdir "%DESTINATION%"
:: Run Robocopy to sync files
robocopy "%SOURCE%" "%DESTINATION%" /E /Z /ZB /R:3 /W:5 /LOG+:"D:\Backups\backup_log.txt"
exit- Replace
C:\Users\YourUsername\Documentswith the path to the folder you want to back up. - Replace
D:\Backups\Documentswith your destination path. - Click File > Save As.
- Set Save as type to All Files (.).
- Name the file
backup.batand save it in a secure location (e.g.,C:\Scripts\backup.bat).
Robocopy Parameters Explained:
/E: Copies subdirectories, including empty ones./Z: Copies files in restartable mode (resumes interrupted transfers)./ZB: Uses restartable mode; if access is denied, uses backup mode./R:3: Retries failed copies up to 3 times./W:5: Waits 5 seconds between retries./LOG+: Appends status logs to the specified file for troubleshooting.
Step 2: Test the Script
Before scheduling, verify the script functions properly: 1.
Double-click your saved backup.bat file. 2. Check your
destination directory to confirm all files copied successfully. 3. Open
the generated log file (backup_log.txt) to ensure there
were no errors.
Step 3: Schedule the Script in Windows 11
- Press Windows Key + S, search for Task Scheduler, and press Enter.
- In the right-hand Actions panel, click Create Basic Task.
- Name the task (e.g.,
Daily Backup) and click Next. - Choose your trigger frequency (e.g., Daily or Weekly) and click Next.
- Set the start time and recurrence settings, then click Next.
- Select Start a program and click Next.
- In the Program/script field, click
Browse and select your
backup.batfile. - In the Start in (optional) field, enter the
directory path containing your script (e.g.,
C:\Scripts\). Click Next. - Review the summary, check the box for Open the Properties dialog for this task when I click Finish, and click Finish.
Step 4: Configure Advanced Permissions
- In the General tab of the Properties window, select Run whether user is logged on or not.
- Check Run with highest privileges to avoid permission errors.
- Under the Conditions tab, uncheck Start the task only if the computer is on AC power if using a laptop.
- Click OK and enter your Windows user credentials when prompted.
Your Windows 11 system will now automatically run the backup script at your chosen interval.