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

  1. Open Notepad (or any text editor).
  2. 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
  1. Replace C:\Users\YourUsername\Documents with the path to the folder you want to back up.
  2. Replace D:\Backups\Documents with your destination path.
  3. Click File > Save As.
  4. Set Save as type to All Files (.).
  5. Name the file backup.bat and save it in a secure location (e.g., C:\Scripts\backup.bat).

Robocopy Parameters Explained:


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

  1. Press Windows Key + S, search for Task Scheduler, and press Enter.
  2. In the right-hand Actions panel, click Create Basic Task.
  3. Name the task (e.g., Daily Backup) and click Next.
  4. Choose your trigger frequency (e.g., Daily or Weekly) and click Next.
  5. Set the start time and recurrence settings, then click Next.
  6. Select Start a program and click Next.
  7. In the Program/script field, click Browse and select your backup.bat file.
  8. In the Start in (optional) field, enter the directory path containing your script (e.g., C:\Scripts\). Click Next.
  9. 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

  1. In the General tab of the Properties window, select Run whether user is logged on or not.
  2. Check Run with highest privileges to avoid permission errors.
  3. Under the Conditions tab, uncheck Start the task only if the computer is on AC power if using a laptop.
  4. 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.