Why Use Robocopy for Backups?
Robocopy (Robust File Copy) has been included with Windows since Vista and is available on all versions of Windows Server 2022. Unlike simple xcopy or File Explorer drag-and-drop, Robocopy is designed for reliable, resumable file transfers with granular control over which files are copied, how attributes are preserved, and what happens when errors occur. Its mirror mode (/MIR) makes it ideal for incremental backups that maintain an exact replica of a source directory tree on the destination. Combined with Windows Task Scheduler, Robocopy forms a zero-cost, highly flexible backup solution for file data on Windows Server 2022.
Robocopy does not create compressed archives like traditional backup tools. Instead, it maintains a live copy of files in their original format, making individual file recovery instant — simply navigate to the destination and copy the file back. For archive-style backups requiring versioning, Robocopy can be combined with timestamped destination folders.
Core Robocopy Syntax
The basic Robocopy command structure is:
robocopy [...] []
A minimal example copying a folder to a backup destination:
robocopy "C:Data" "D:BackupData" /E /COPYALL /LOG:"D:Logsrobocopy_data.log"
This copies all files and subdirectories including empty ones (/E), preserves all file attributes including timestamps, owner, ACLs, and audit information (/COPYALL), and writes a log file.
Essential Robocopy Flags Explained
/MIR (Mirror): Mirrors the source to the destination, deleting files at the destination that no longer exist at the source. This is the most important flag for incremental backups because it only copies files that are new or modified since the last run, keeping the backup in exact sync with the source. Use with caution — files deleted at the source will also be deleted from the backup.
robocopy "C:Data" "D:BackupData" /MIR /LOG:"D:Logsmirror.log"
/E: Copies all subdirectories, including empty ones. Without this flag, Robocopy copies only the top-level directory. /E is implied when using /MIR, so the two are not usually combined.
/COPYALL: Copies all file information: Data (D), Attributes (A), Timestamps (T), NTFS ACLs (S), Owner info (O), and Auditing info (U). This is equivalent to /COPY:DATSOU. Requires the running account to have SeBackupPrivilege, which administrators have by default.
robocopy "C:Data" "D:BackupData" /E /COPYALL
/LOG and /LOG+: Writes output to a log file. /LOG:"path" overwrites the log each run. /LOG+:"path" appends to the existing log, preserving history across runs.
robocopy "C:Data" "D:BackupData" /MIR /LOG+:"D:Logsrobocopy.log" /NP
The /NP flag suppresses the percentage progress output in the log, keeping log files compact.
/R and /W: Control retry behaviour. /R:3 retries failed file copies 3 times. /W:10 waits 10 seconds between retries. The defaults are 1 million retries with a 30-second wait, which is impractical. Always override these:
robocopy "C:Data" "D:BackupData" /MIR /R:3 /W:10
/MT (Multithreaded): Runs file copies using multiple threads. The default is /MT:8 (8 threads). For fast local disks or high-bandwidth networks, increasing this can dramatically improve throughput. On slower links, reduce threads to avoid overwhelming the connection:
robocopy "C:Data" "\fileserverBackup" /MIR /MT:16 /R:3 /W:5
/Z (Restartable Mode): Enables restartable mode, saving the copy state so interrupted transfers can be resumed. Useful over slow or unreliable network connections:
robocopy "C:Data" "\nas01BackupData" /MIR /Z /R:5 /W:15
/B (Backup Mode): Uses the Windows Backup API to copy files even if the running account does not have read permission on the file. Requires SeBackupPrivilege. Combine with /COPYALL for full backup fidelity:
robocopy "C:SensitiveData" "D:BackupSensitive" /MIR /B /COPYALL /R:3 /W:5
Excluding Files and Directories
Robocopy supports excluding specific files and directories from the copy operation using /XF and /XD:
/XF (Exclude Files): Excludes files matching specified names or wildcards:
robocopy "C:Data" "D:BackupData" /MIR /XF "*.tmp" "*.log" "thumbs.db" /R:3 /W:5
/XD (Exclude Directories): Excludes directories matching specified names:
robocopy "C:Data" "D:BackupData" /MIR /XD "Temp" "Cache" ".git" /R:3 /W:5
Combining both exclusions for a practical backup scenario:
robocopy "C:UsersSharedDocs" "D:BackupSharedDocs" /MIR /COPYALL `
/XF "*.tmp" "~*" "desktop.ini" `
/XD "AppData" "Temp" "System Volume Information" `
/R:3 /W:10 /LOG+:"D:Logsshareddocs_backup.log" /NP /TS
The /TS flag adds timestamps to each log line, making it easy to correlate log entries with specific runs.
Using Robocopy with Network Paths and Credentials
Robocopy itself does not accept username/password parameters. When copying to a remote server, you must first establish credentials using a mapped drive or net use:
# Map a network drive with credentials before running robocopy
net use Z: \backupserverBackupShare /user:DOMAINbackupuser P@ssw0rd!
# Now run robocopy against the mapped drive
robocopy "C:Data" "Z:ServerData" /MIR /COPYALL /R:3 /W:10 /LOG:"D:Logsbackup.log" /NP
# Disconnect after backup completes
net use Z: /delete
In a scheduled task script, wrap these three steps in a PowerShell script for atomicity.
Scheduling Robocopy with Task Scheduler
To run a Robocopy backup on a schedule, create a PowerShell wrapper script and schedule it with Windows Task Scheduler. First, create the script at C:ScriptsRunBackup.ps1:
# C:ScriptsRunBackup.ps1
$source = "C:Data"
$destination = "D:BackupData"
$logFile = "D:Logsbackup_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
$roboArgs = @(
$source,
$destination,
"/MIR",
"/COPYALL",
"/R:3",
"/W:10",
"/MT:8",
"/NP",
"/TS",
"/LOG:$logFile",
"/XF", "*.tmp", "~*",
"/XD", "Temp", "Cache"
)
$exitCode = (Start-Process robocopy -ArgumentList $roboArgs -Wait -PassThru).ExitCode
Exit $exitCode
Register the scheduled task to run daily at 11:00 PM:
$action = New-ScheduledTaskAction -Execute "powershell.exe" `
-Argument "-NonInteractive -ExecutionPolicy Bypass -File C:ScriptsRunBackup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At "23:00"
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hours 4) -StartWhenAvailable
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask `
-TaskName "DailyRobocopyBackup" `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Principal $principal `
-Description "Daily incremental backup using Robocopy"
Running as SYSTEM avoids credential expiry issues. Ensure the SYSTEM account has read access to the source and read/write access to the destination.
Robocopy Exit Codes
Robocopy returns numeric exit codes that indicate the outcome of the operation. These are bit flags — multiple values can be combined:
0 = No files were copied (source and destination are in sync). 1 = Files were copied successfully. 2 = Extra files in destination detected (no error). 4 = Mismatched files detected. 8 = Some files or directories could not be copied. 16 = Fatal error — no files copied.
Exit codes 0–7 are generally considered success (no errors, some files may or may not have been copied). Exit codes 8 and above indicate errors. In a PowerShell script, check the exit code:
$exitCode = $LASTEXITCODE
if ($exitCode -ge 8) {
Write-Error "Robocopy reported errors. Exit code: $exitCode"
# Trigger alert
} elseif ($exitCode -eq 0) {
Write-Host "No changes detected — backup is current."
} else {
Write-Host "Backup completed. Exit code: $exitCode"
}
Email Notification After Robocopy
Add email notification to your PowerShell backup script to alert administrators on completion or failure:
# Add to the end of RunBackup.ps1
$smtpServer = "mail.yourdomain.com"
$from = "[email protected]"
$to = "[email protected]"
$subject = if ($exitCode -ge 8) { "BACKUP FAILED — RobocopyBackup ExitCode $exitCode" } `
else { "Backup Completed — ExitCode $exitCode" }
$body = Get-Content $logFile -Tail 50 | Out-String
Send-MailMessage `
-SmtpServer $smtpServer `
-From $from `
-To $to `
-Subject $subject `
-Body $body
Robocopy for Cross-Server Sync
Robocopy is well-suited for synchronising data between two Windows Server 2022 file servers, for example, replicating a primary file share to a secondary server for manual failover or geographic distribution:
robocopy "\fileserver01SharedDocs" "\fileserver02SharedDocs-DR" `
/MIR /COPYALL /SEC /SECFIX `
/R:5 /W:10 /MT:16 /Z `
/LOG+:"D:Logscrossserver_sync.log" /NP /TS /ETA
The /SEC flag copies NTFS security attributes. /SECFIX fixes file security on all files, even those that were not copied. The /ETA flag shows estimated time of arrival for each file in the console output. For production cross-server sync, consider combining Robocopy with DFS Replication (DFSR) for real-time replication or using Storage Replica for block-level sync — Robocopy is best suited for scheduled batch sync rather than real-time replication.