Robocopy examples

Sync target with source (will delete files in target)

robocopy "{:Source:}" "{:Target:}" /mir /copy:{:CopyFlags:} /r:{:RetryCount:} /w:{:WaitTime:}

Sync target with source (will delete files in target) and log the results

robocopy "{:Source:}" "{:Target:}" /mir /copy:{:CopyFlags:} /r:{:RetryCount:} /w:{:WaitTime:} /tee /np "/{:LoggingMode:}:{:LogFileFullPath:}"

Common configuration switches

Switch Description
/S Copy subdirectories
/E Copy subdirectories (including empty ones)
/MIR Mirror directory tree (delete files/folders from target that no longer exist in source)
/Z Copy files in restartable mode
/ZB Copy files in restartable mode but fail back to backup mode if access denied
/R:{:RetryCount:} Retry {:RetryCount:} times (default is 1000000)
/W:{:WaitTime:} Wait {:WaitTime:} seconds between retries (default is 30 seconds)
/TBD Wait for share names to be defined (for retry error 67)
/NP No progress to be displayed for file copies
/V Verbose output (including skipped files)
/TEE Output to console as well as output as well as log file
/XF “{:ExcludeFiles:}” Exclude files matching {:ExcludeFiles:} (can use * wildcard)
/XD “{:ExcludeDirs:}” Exclude directories matching {:ExcludeDirs:} (can use * wildcard)
/LOG:{:LogFileFullPath:} Output results to log file (be sure to properly escape whitespace when using this)
/LOG+:{:LogFileFullPath:} Append results to log file (be sure to properly escape whitespace when using this)
/UNILOG:{:LogFileFullPath:} Output results to log file (be sure to properly escape whitespace when using this)
/UNILOG+:{:LogFileFullPath:} Append results to log file (be sure to properly escape whitespace when using this)

For a more complete reference I recommend: https://ss64.com/nt/robocopy.html

Regex search for errors in output log

If you would like to search your output log for errors I have used the below regex (tested in Notepad++):

ERROR \d+ \(0x\d+\)

Unicode file notes

When using Unicode logging on some older systems the output file may appear to be gibberish. The PoweShell fix I have used for this (source here) is:

$bytes = [System.IO.File]::ReadAllBytes('C:\Temp\RoboCopy.log')
$len = $bytes.Length
$text = [System.Text.Encoding]::ASCII.GetString($bytes,2,$len -2)
$text | Out-File -FilePath 'C:\Temp\RoboCopyFixed.log'