Robocopy keeps retrying a locked file one million times, 30 seconds apart, unless you tell it otherwise. That default alone has turned plenty of overnight copy jobs into week-long hangs, and it’s why the first thing this robocopy cheat sheet gives you is a set of flags you can paste without surprises: mirror, copy with ACLs, sane retries, multithreading, logging, exclusions and the exit codes your scripts need to read.
The tables come first, then the commands that need more than a table row: /MIR without losing data, permission copies, /MT tuning and the exit code check that stops Task Scheduler from reporting a good copy as a failure. Every switch here was checked against Microsoft’s robocopy reference in November 2026 for Windows 11 and Windows Server 2025.
Robocopy commands cheat sheet
Robocopy takes a source folder, a destination folder, an optional file pattern and then options, in that order: robocopy <source> <destination> [file ...] [options]. Leave the file pattern out and it copies *.*. Switches aren’t case sensitive, so /MIR and /mir do the same thing.
Copy, mirror and move
| Switch | What it does |
|---|---|
/S | Copy subfolders, skip empty ones |
/E | Copy subfolders, including empty ones |
/LEV:n | Copy only the top n levels of the tree |
/MIR | Mirror the tree (/E plus /PURGE) |
/PURGE | Delete destination files that no longer exist in the source |
/MOV | Move files (delete from source after copy) |
/MOVE | Move files and folders |
/Z | Restartable mode, resumes a broken file copy |
/B | Backup mode, bypasses ACLs that would block reads |
/ZB | Restartable mode, falls back to backup mode on access denied |
/J | Unbuffered I/O, meant for large files |
/CREATE | Build the folder tree with zero-length files only |
File properties and permissions
| Switch | What it does |
|---|---|
/COPY:DAT | Data, attributes, timestamps (the default) |
/COPY:DATSOU | Add NTFS ACLs (S), owner (O) and auditing (U) |
/SEC | Same as /COPY:DATS |
/COPYALL | Same as /COPY:DATSOU |
/NOCOPY | Copy no file information (pair with /PURGE) |
/SECFIX | Fix security on every file, even skipped ones |
/TIMFIX | Fix timestamps on every file, even skipped ones |
/DCOPY:DAT | Keep folder timestamps (default is /DCOPY:DA) |
/A+:R / /A-:R | Add or strip an attribute on copied files |
/SL | Copy symbolic links as links, not their targets |
/SJ | Copy junctions as junctions |
Filter and exclude
| Switch | What it does |
|---|---|
/XF *.tmp ~$* | Exclude files by name, path or wildcard |
/XD node_modules .git | Exclude folders by name or path |
/XO | Skip source files older than the destination copy |
/XN | Skip source files newer than the destination copy |
/XC | Skip files with the same timestamp but a different size |
/XX | Leave extra destination files alone (no delete, no report) |
/XL | Skip files that only exist in the source |
/XJ | Skip junction points |
/MAXAGE:7 | Skip files last modified more than 7 days ago |
/MINAGE:20260101 | Skip files modified after that date |
/MAX:n / /MIN:n | Skip files bigger or smaller than n bytes |
/IA:H / /XA:H | Include or exclude files with an attribute (here hidden) |
/FFT | Two-second timestamp tolerance for FAT and many NAS shares |
/DST | Ignore one-hour daylight saving differences |
Retries, speed and throttling
| Switch | What it does |
|---|---|
/R:n | Retries per failed file (default 1,000,000) |
/W:n | Seconds between retries (default 30) |
/REG | Save the current /R and /W as defaults for the current user |
/TBD | Wait for a share name to appear (retries error 67) |
/MT:n | Copy with n threads, 1 to 128 (default 8 when n is left out) |
/IPG:n | Inter-packet gap in ms for slow links (not with /MT) |
/IORATE:10m | Cap I/O at 10 MB per second |
/THRESHOLD:100m | Only throttle files bigger than 100 MB |
/COMPRESS | Ask for SMB network compression |
/LFSM | Pause when destination free space drops under 10% |
/RH:2200-0600 | Only start new copies between 22:00 and 06:00 |
/MOT:m / /MON:n | Stay running and recopy once m minutes have passed and n changes are seen (the unset one defaults to 1) |
Logging and jobs
| Switch | What it does |
|---|---|
/L | List only: show what would happen, change nothing |
/LOG:file | Write a log, overwrite the old one |
/LOG+:file | Append to an existing log |
/UNILOG:file | Unicode log, for non-ASCII file names |
/TEE | Print to the console as well as the log |
/NP | No per-file percentage counter |
/NFL / /NDL | Leave file names or folder names out of the log |
/FP | Log full paths |
/TS | Log source timestamps |
/V | Verbose, also lists skipped files |
/X | Report all extra files, not only selected ones |
/BYTES | Sizes in bytes instead of k, m, g |
/NJH / /NJS | No job header or no job summary |
/ETA | Show the estimated finish time per file |
/SAVE:name | Save the options to a job file |
/JOB:name | Run with the options from a saved job file |
/QUIT | Stop after parsing the command line |
The full option list lives in Microsoft’s robocopy command reference. For the rest of Command Prompt (diskpart, netsh, services and users), the CMD commands cheat sheet covers it in the same table layout.
Copy a folder tree
A plain copy of a whole tree needs /E so empty folders come along, /Z so a dropped connection resumes the file instead of restarting it, and short retries. Add /DCOPY:DAT as well, because the default only copies folder data and attributes, so every folder on the destination ends up stamped with today’s date.
The commands in this guide are written for an elevated Command Prompt. In PowerShell, wrap any argument holding $ in single quotes ('~$*', '$RECYCLE.BIN') or PowerShell expands it as a variable. Robocopy creates a log file but not its folder, so make the log and script folders once with mkdir C:\Logs C:\Scripts (in PowerShell, mkdir C:\Logs, C:\Scripts) before the first /LOG run.
robocopy D:\Projects E:\Backup\Projects /E /Z /DCOPY:DAT /R:2 /W:5
Running that a second time copies only what changed. Robocopy compares size and timestamp and skips files that match, so a repeat run over an unchanged tree has nothing to copy and should exit with code 0.
To copy a single file or a pattern, put it after the destination. The folder paths stay folders; robocopy never takes a file path as its source argument.
robocopy C:\Reports \\fs01\archive yearly-report.xlsx *.pdf /Z
Paths with spaces need double quotes, and a quoted path must not end in a backslash. "C:\My Data\" breaks because the trailing \" is read as an escaped quote, so robocopy sees one long garbled source path. Write "C:\My Data" instead.
Mirror a folder with /MIR
A robocopy mirror with /MIR makes the destination an exact copy of the source, which means it deletes anything on the destination that isn’t in the source. Point it at the wrong destination, or swap the two arguments, and it wipes data without asking. Always preview the run with /L first.
robocopy D:\Shares\Finance \\nas01\backup\Finance /MIR /L /NJH /FP /LOG:C:\Logs\finance-preview.log
Open the log and search for lines marked *EXTRA File and *EXTRA Dir. Those are the items the real run will delete. If the list holds anything you want to keep, stop and fix the destination before you drop /L.
robocopy D:\Shares\Finance \\nas01\backup\Finance /MIR /Z /DCOPY:DAT /R:2 /W:5 /MT:16 /NP /LOG:C:\Logs\finance.log
Mirroring the root of a volume used to touch System Volume Information. Current builds skip that folder at the top level of both sides, but copying from a drive root still gives the destination folder the hidden and system attributes. Clear them afterwards with attrib -h -s E:\Backup or mirror a subfolder instead. If you do mirror a whole volume with /B, add /XD "System Volume Information" "$RECYCLE.BIN". Bare folder names match on both sides, so backup mode never tries to copy or purge those folders.
There is one more difference between /MIR and /E /PURGE, which otherwise delete the same files. With /MIR, robocopy overwrites the security on the destination root folder; with /E /PURGE, it leaves that folder’s ACL alone. Use the second form when the backup share has its own permissions you set by hand.
Copy NTFS permissions and owners
Moving a file share to a new server is where robocopy beats Explorer, because it can carry the NTFS ACLs, the owner and the audit settings along with the data. Run these from an elevated prompt. Setting arbitrary owners and auditing entries needs the backup, restore and security privileges that an elevated administrator token holds. Members of Backup Operators get backup and restore too, which covers /B but not auditing entries. Those privileges only count on the machine that grants them, so the account needs the same rights on the destination server, and it still needs an elevated prompt.
robocopy D:\Shares \\fs02\D$\Shares /MIR /COPYALL /DCOPY:DAT /B /R:1 /W:1 /MT:32 /NP /LOG:C:\Logs\migrate.log
Each flag in that line has a job during a migration:
/COPYALLfor data, attributes, timestamps, ACLs, owner and auditing/Bto read files whose ACL locks out even administrators/DCOPY:DATfor folder timestamps/R:1 /W:1so locked files fail fast and land in the log
Run the same line again at cutover, after users are off the share. The second pass only moves what changed, so the outage window is the length of the delta, not the full copy. If you only need permissions without owner and auditing, /SEC is the shorter form.
Permissions changed on the source after the first pass don’t come across on a rerun, because robocopy skips files whose data and timestamps match. Add /SECFIX to that rerun so it rewrites security on skipped files too. It needs one of /COPYALL, /SEC, /COPY:S, /COPY:O or /COPY:U alongside it to know which security to fix.
robocopy D:\Shares \\fs02\D$\Shares /MIR /COPYALL /SECFIX /B /R:1 /W:1 /MT:32 /NP /LOG+:C:\Logs\migrate.log
Quotas and file screens don’t travel with the data. If the old server ran FSRM, rebuild them on the new one using the steps in the FSRM quotas and file screens guide before users land on the new share.
Set sane retries
The defaults are /R:1000000 and /W:30. One file that stays locked, say a PST that Outlook holds open, will be retried for 30 million seconds, roughly 347 days, and the job looks frozen the whole time. Pick retry values that match the link instead.
| Situation | Retry flags |
|---|---|
| Local disk to local disk | /R:1 /W:1 |
| LAN share, nightly job | /R:2 /W:5 |
| VPN or Wi-Fi that drops | /Z /R:10 /W:30 |
| Share that comes up after boot | /TBD /R:5 /W:60 |
/REG saves the current /R and /W as defaults under HKCU\Software\Microsoft\ResKit\Robocopy, according to the SS64 robocopy page, so they apply to your user account only. A scheduled task that runs as SYSTEM or a service account doesn’t read your HKCU and falls back to the million retries, so scheduled jobs should always spell the values out.
robocopy C:\Windows\Web C:\RegTest /R:2 /W:5 /REG /L
The /L in that line is there so nothing gets copied while you set the defaults. Check that later runs under your account show /R:2 /W:5 on the Options : line of the job header before you rely on it.
Speed up copies with /MT
/MT copies several files at once. Bare /MT means 8 threads and the range is 1 to 128. It helps most on trees with thousands of small files over SMB, where per-file round trips cost more than the bytes. A few huge files on one disk gain little, because the disk is already the bottleneck.
robocopy D:\Builds \\nas01\builds /E /MT:32 /R:2 /W:5 /NP /NFL /NDL /LOG:C:\Logs\builds.log
Send output to a log when you use /MT. Microsoft’s own reference recommends redirecting output with /LOG for better performance when /MT is on. /NP, /NFL and /NDL cut the log to errors and the summary, which also makes failures easy to find.
Some options refuse to work alongside multithreading:
/IPG(use/IORATEto throttle instead)/EFSRAWfor EFS-encrypted files/LFSMlow free space mode
For single large files such as VHDX images or ISO libraries, /J (unbuffered I/O) is the flag to try before more threads, since it skips the system file cache that large sequential copies fill for no gain. Measure both on your own hardware, because the right thread count depends on the disks and the network in between.
robocopy E:\VMs \\hv02\D$\VMs *.vhdx /J /Z /R:2 /W:5 /NP /LOG:C:\Logs\vhdx.log
To keep a daytime copy from flooding a branch link, use /IORATE:10m with /THRESHOLD:100m, which caps big files at 10 MB per second and leaves small ones unthrottled. Robocopy won’t throttle below 524,288 bytes per second even if you ask for less.
Exclude files and folders
/XF takes file names or wildcards and /XD takes folder names or full paths. Both accept several values in a row, so list everything after one switch rather than repeating it. A bare name matches at any depth, while a full path excludes only that one folder.
robocopy C:\Users\jdoe \\nas01\home\jdoe /MIR /XD AppData "OneDrive - Contoso" node_modules .git /XF *.tmp ~$* desktop.ini /R:1 /W:1 /LOG:C:\Logs\home.log
Before trusting /MIR with exclusions, run it once with /L and confirm the excluded paths on the destination don’t show up as *EXTRA lines. If you need a guarantee that nothing extra on the destination is ever deleted, use /E with /XX instead of /MIR.
Age filters work well for archive jobs. This one moves PDFs older than 90 days into the archive share and removes them from the source:
robocopy D:\Scans \\nas01\archive\scans *.pdf /S /MINAGE:90 /MOV /R:1 /W:1 /LOG+:C:\Logs\archive.log
Robocopy reads /MINAGE and /MAXAGE as days when the number is under 1900 and as a YYYYMMDD date otherwise. Run the line with /L once before trusting it with /MOV, because a moved file only exists on the archive share afterwards.
Write logs you can read
Every unattended robocopy run needs a log. /LOG overwrites the file on each run and /LOG+ appends, which suits a nightly job that keeps one log per month. Use /UNILOG+ when file names carry accented or non-Latin characters, otherwise they turn into question marks in the log.
robocopy D:\Data \\nas01\backup\data /MIR /R:2 /W:5 /MT:16 /NP /NDL /FP /TS /TEE /LOG+:C:\Logs\data-2026-11.log
The summary table at the end of each run holds the counts that matter: Copied, Skipped, Mismatch, FAILED and Extras for folders, files and bytes. A non-zero FAILED column is the line to alert on, and it maps straight to the exit codes below.
To pull just the failures out of a long log in PowerShell, search for the ERROR lines robocopy writes for each file it gave up on:
Select-String -Path C:\Logs\data-2026-11.log -Pattern 'ERROR \d+ \(0x' | Select-Object -Last 20
More log handling (Select-String, Get-Content -Wait and friends) is in the PowerShell cheat sheet.
Read robocopy exit codes
Robocopy exit codes are a bitmask, and only 8 and above mean something failed. Code 1 is a normal successful copy, which is why Task Scheduler, SCCM and CI runners that treat any non-zero code as an error report good robocopy jobs as broken. Each bit adds one fact about the run:
| Code | Meaning | Treat as |
|---|---|---|
| 0 | Nothing copied, source and destination already match | Success |
| 1 | Files copied | Success |
| 2 | Extra files in the destination, nothing copied | Success |
| 3 | Files copied, extra files present (1+2) | Success |
| 4 | Mismatched files or folders detected | Success, check log |
| 5 | Files copied, some mismatched (1+4) | Success, check log |
| 6 | Extra and mismatched, nothing copied (2+4) | Success, check log |
| 7 | Copied, mismatched and extra (1+2+4) | Success, check log |
| 8 | Some files failed after all retries | Failure |
| 9 to 15 | A failure plus any of the bits above | Failure |
| 16 | Serious error, nothing copied (bad syntax, no access to source or destination) | Failure |
Codes 0 to 3, 5 to 8 and the “8 or higher means failure” rule come from Microsoft’s documentation. Microsoft doesn’t list 4 and 16 in its table; the meanings above follow the bitmask as SS64 documents it, and SS64 lists usage errors such as a mistyped switch as one cause of 16.
In a batch file, test with GEQ 8 and turn the success range into a clean exit 0. Save this as C:\Scripts\mirror-data.cmd:
notepad C:\Scripts\mirror-data.cmd
Paste the script, which runs the mirror and maps robocopy’s codes to the 0 or 1 a scheduler expects:
@echo off
robocopy D:\Data \\nas01\backup\data /MIR /R:2 /W:5 /MT:16 /NP /NDL /LOG+:C:\Logs\data.log
set RC=%ERRORLEVEL%
if %RC% GEQ 8 (
echo robocopy failed with exit code %RC%
exit /b 1
)
exit /b 0
Capture %ERRORLEVEL% into a variable on the line straight after robocopy, as above. Inside a parenthesised block, %ERRORLEVEL% is expanded when the whole block is parsed, not when each line runs, so reading it there gives you a stale value.
PowerShell reads the code from $LASTEXITCODE. The same mapping in a script looks like this:
robocopy D:\Data \\nas01\backup\data /MIR /R:2 /W:5 /MT:16 /NP /NDL /LOG+:C:\Logs\data.log
$rc = $LASTEXITCODE
if ($rc -ge 8) {
Write-Error "robocopy failed with exit code $rc"
exit 1
}
exit 0
Task Scheduler shows the raw code in hex in its Last Run Result column, so a plain robocopy task that copied files shows 0x1. Wrapping it in one of these scripts turns that into 0x0 and leaves real failures visible.
Schedule a nightly mirror
Long option lists are easy to get wrong in a scheduler field. Save them to a job file once with /SAVE, then call the job by name. All copy, retry and logging options have to come before /SAVE. Anything after /SAVE is left out of the job file, so put /QUIT right after it when you only want the file written and nothing copied. The job lands in data-mirror.RCJ, a plain text file you can edit.
robocopy D:\Data \\nas01\backup\data /MIR /R:2 /W:5 /MT:16 /NP /NDL /LOG+:C:\Logs\data.log /SAVE:C:\Scripts\data-mirror /QUIT
From then on, robocopy /JOB:C:\Scripts\data-mirror runs the same copy, and you can still add switches after it for a one-off run, such as /L to preview. Swap the long robocopy line in the wrapper script from the previous section for that /JOB call so the options live in one place, then register the wrapper as a daily task:
schtasks /create /tn "Mirror D Data to NAS" /tr "C:\Scripts\mirror-data.cmd" /sc daily /st 02:00 /ru SYSTEM /rl HIGHEST
A task that runs as SYSTEM reaches network shares as the computer account (DOMAIN\FS01$), so grant that account write access on the share. That only works on domain-joined file servers; for a standalone NAS, run the task as an account the NAS knows. Failed runs also land in the Task Scheduler operational log, and the Windows event IDs cheat sheet lists the IDs worth alerting on.
Fix common robocopy errors
Robocopy prints each per-file failure as ERROR <n> (0x<hex>), followed by the action it was attempting and the Windows error text. The number is a standard Win32 error code, so the same code means the same thing across Windows 11 and Server 2025.
ERROR 5 (0x00000005) Access is denied
The account running robocopy can’t read the source file or write the destination. Open an elevated prompt first, since a non-elevated administrator token lacks the backup and restore privileges. If it still fails, add /ZB so robocopy switches to backup mode on denied files. When the error appears while copying security, drop to /COPY:DAT to confirm the data side works, then fix rights on the destination.
ERROR 32 (0x00000020) The process cannot access the file because it is being used by another process
Another program holds the file open with a lock that blocks reads, which is common with PST files, database files and open VHDX disks. Robocopy can’t copy through that lock because it doesn’t use the Volume Shadow Copy Service, as the Wikipedia entry on robocopy points out; take a shadow copy with DiskShadow first if you need a live volume. Keep /R and /W low so the job moves on, schedule the copy when the application is closed, or exclude the file with /XF and back it up with the application’s own tool.
ERROR 67 (0x00000043) The network name cannot be found
The share name in the UNC path doesn’t exist on that server, or the server isn’t sharing it yet. Check it with net view \\nas01. If the share comes up late, for example a NAS that boots after the Windows host, add /TBD so robocopy waits and retries instead of failing straight away.
Files copy again on every run
Robocopy decides what changed from size and timestamp. Many NAS file systems and FAT or exFAT drives store times with two-second precision, so NTFS times never match exactly and every file looks newer. Add /FFT for those targets, and /DST if the copies started after a daylight saving change.
The job hangs on one file
It almost always means the default retry count is still in play. Press Ctrl+C, add /R:2 /W:5 to the command, and rerun it. The files already copied are skipped, so the rerun picks up where the first one stopped.
Robocopy ships with every current Windows build, including the ones covered in the Windows Server 2025 upgrade guide, so nothing here needs installing. Start with /L on any command that deletes or moves, keep the retries short, and let the exit code decide whether the job failed. The other sheets in the cheat sheet collection follow the same layout.