How to Use Robocopy for Scheduled File Backups on Windows Server 2016
Robocopy, short for Robust File Copy, is a command-line utility included with Windows Server 2016 that provides highly reliable file synchronization and backup capabilities far beyond what the basic xcopy command offers. Robocopy supports multithreaded copying, can resume interrupted transfers from the point of failure, mirrors directory structures including NTFS permissions and timestamps, handles VSS (Volume Shadow Copy Service) integration for backing up open files, and provides detailed logging. When combined with Windows Task Scheduler, Robocopy becomes a robust, zero-cost scheduled backup solution suitable for backing up file shares, user profiles, application data directories, and more. This guide covers Robocopy’s key options, creating a complete backup script, scheduling it as a task, and reviewing logs for operational assurance.
No additional installation is required. Robocopy is available in all versions of Windows Server 2016 at C:WindowsSystem32robocopy.exe. You need an account with read access to the source and write access to the destination. For backing up open files, you will need the /B (backup mode) switch, which requires the Backup Files and Directories user right on the source server.
Step 1: Understand Key Robocopy Switches
Before building a script, understand the most important Robocopy options. The /MIR switch mirrors the source to the destination, deleting files at the destination that no longer exist at the source — use this only when you want the destination to be an exact replica. The /E switch copies all subdirectories including empty ones without deleting extras at the destination, which is safer for incremental backups. The /Z switch enables restart mode, which allows large file copies to be resumed after interruption. The /B switch runs in backup mode, bypassing access controls to copy files that would otherwise be inaccessible. The /MT:n switch enables multithreaded copying with n threads, dramatically increasing throughput on fast networks or local SSDs. The /LOG switch writes a log file and /LOG+ appends to an existing log.
Step 2: Create a Basic Mirror Backup Script
Create a PowerShell script named DailyRobocopy.ps1 in C:AdminScripts. The script below mirrors a source directory to a backup destination, uses 8 threads, runs in backup mode, logs results, and sends an alert email on failure:
param(
[string]$Source = "D:SharedData",
[string]$Destination = "\BACKUPSERVERBackupsSharedData",
[string]$LogFile = "C:AdminScriptsLogsrobocopy.log",
[string]$SmtpServer = "smtp.yourdomain.com",
[string]$AlertTo = "[email protected]"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $LogFile -Value "`n=== Backup started at $timestamp ==="
robocopy $Source $Destination /MIR /Z /B /MT:8 /R:3 /W:10 /LOG+:$LogFile /NP /NDL
$rc = $LASTEXITCODE
Add-Content -Path $LogFile -Value "Robocopy exit code: $rc at $(Get-Date -Format 'HH:mm:ss')"
# Exit codes 0-7 are success or informational; 8+ indicate errors
if ($rc -ge 8) {
Send-MailMessage -To $AlertTo -From "[email protected]" `
-Subject "ROBOCOPY FAILURE on $env:COMPUTERNAME (RC=$rc)" `
-Body "Backup of $Source to $Destination failed. Exit code: $rc. See $LogFile." `
-SmtpServer $SmtpServer
}
Step 3: Add VSS Shadow Copy Support for Open Files
By default, Robocopy in backup mode (/B) can read most open files by requesting a backup read. However, for files held open with exclusive locks by applications such as databases, use the /EFSRAW switch or initiate a VSS shadow copy before running Robocopy. The following approach creates a VSS snapshot, maps it to a drive letter, runs Robocopy against the snapshot, then removes it:
$shadow = (Get-WmiObject -List Win32_ShadowCopy).Create("D:", "ClientAccessible")
$shadowID = $shadow.ShadowID
$shadowPath = (Get-WmiObject Win32_ShadowCopy | Where-Object { $_.ID -eq $shadowID }).DeviceObject
cmd /c "mklink /d C:ShadowMount `"$shadowPath`""
robocopy C:ShadowMountSharedData \BACKUPSERVERBackupsSharedData /MIR /Z /MT:8 /R:3 /W:10 /LOG+:$LogFile /NP
cmd /c "rd C:ShadowMount"
(Get-WmiObject Win32_ShadowCopy | Where-Object { $_.ID -eq $shadowID }).Delete()
Step 4: Implement Incremental Backups with the /MAXAGE Switch
For very large data sets where a full mirror backup is too slow for a nightly window, use Robocopy in incremental mode to copy only files modified within the last day:
robocopy D:SharedData \BACKUPSERVERBackupsIncremental /E /Z /B /MT:8 /MAXAGE:1 /R:3 /W:10 /LOG+:C:AdminScriptsLogsrobocopy-inc.log /NP
Store incremental backups in date-stamped subfolders by using a PowerShell variable for the destination path:
$dateStamp = Get-Date -Format "yyyy-MM-dd"
robocopy D:SharedData "\BACKUPSERVERBackupsDaily$dateStamp" /E /MAXAGE:1 /MT:8 /LOG+:$LogFile /NP
Step 5: Schedule the Backup Script as a Task
Register the script as a daily scheduled task running at 1:00 AM:
$action = New-ScheduledTaskAction -Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -NonInteractive -File C:AdminScriptsDailyRobocopy.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At "01:00AM"
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hours 6) -StartWhenAvailable
Register-ScheduledTask -TaskName "RobocopyDailyBackup" `
-Action $action -Trigger $trigger -Settings $settings `
-RunLevel Highest -User "SYSTEM"
Step 6: Review and Rotate Log Files
Robocopy log files grow over time. Add a log rotation block to the script to retain only the last 30 days of logs:
Get-ChildItem "C:AdminScriptsLogs" -Filter "robocopy*.log" |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
Remove-Item -Force
Robocopy’s combination of reliability, multithreaded performance, open-file handling, and detailed logging makes it an excellent choice for scheduled file backup workflows on Windows Server 2016, particularly for environments where simplicity and zero licensing cost are priorities.