Cheat Sheets

Robocopy Commands Cheat Sheet: Mirror, ACLs, Exit Codes

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.

Original content from computingforgeeks.com - post 194215

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

SwitchWhat it does
/SCopy subfolders, skip empty ones
/ECopy subfolders, including empty ones
/LEV:nCopy only the top n levels of the tree
/MIRMirror the tree (/E plus /PURGE)
/PURGEDelete destination files that no longer exist in the source
/MOVMove files (delete from source after copy)
/MOVEMove files and folders
/ZRestartable mode, resumes a broken file copy
/BBackup mode, bypasses ACLs that would block reads
/ZBRestartable mode, falls back to backup mode on access denied
/JUnbuffered I/O, meant for large files
/CREATEBuild the folder tree with zero-length files only

File properties and permissions

SwitchWhat it does
/COPY:DATData, attributes, timestamps (the default)
/COPY:DATSOUAdd NTFS ACLs (S), owner (O) and auditing (U)
/SECSame as /COPY:DATS
/COPYALLSame as /COPY:DATSOU
/NOCOPYCopy no file information (pair with /PURGE)
/SECFIXFix security on every file, even skipped ones
/TIMFIXFix timestamps on every file, even skipped ones
/DCOPY:DATKeep folder timestamps (default is /DCOPY:DA)
/A+:R / /A-:RAdd or strip an attribute on copied files
/SLCopy symbolic links as links, not their targets
/SJCopy junctions as junctions

Filter and exclude

SwitchWhat it does
/XF *.tmp ~$*Exclude files by name, path or wildcard
/XD node_modules .gitExclude folders by name or path
/XOSkip source files older than the destination copy
/XNSkip source files newer than the destination copy
/XCSkip files with the same timestamp but a different size
/XXLeave extra destination files alone (no delete, no report)
/XLSkip files that only exist in the source
/XJSkip junction points
/MAXAGE:7Skip files last modified more than 7 days ago
/MINAGE:20260101Skip files modified after that date
/MAX:n / /MIN:nSkip files bigger or smaller than n bytes
/IA:H / /XA:HInclude or exclude files with an attribute (here hidden)
/FFTTwo-second timestamp tolerance for FAT and many NAS shares
/DSTIgnore one-hour daylight saving differences

Retries, speed and throttling

SwitchWhat it does
/R:nRetries per failed file (default 1,000,000)
/W:nSeconds between retries (default 30)
/REGSave the current /R and /W as defaults for the current user
/TBDWait for a share name to appear (retries error 67)
/MT:nCopy with n threads, 1 to 128 (default 8 when n is left out)
/IPG:nInter-packet gap in ms for slow links (not with /MT)
/IORATE:10mCap I/O at 10 MB per second
/THRESHOLD:100mOnly throttle files bigger than 100 MB
/COMPRESSAsk for SMB network compression
/LFSMPause when destination free space drops under 10%
/RH:2200-0600Only start new copies between 22:00 and 06:00
/MOT:m / /MON:nStay running and recopy once m minutes have passed and n changes are seen (the unset one defaults to 1)

Logging and jobs

SwitchWhat it does
/LList only: show what would happen, change nothing
/LOG:fileWrite a log, overwrite the old one
/LOG+:fileAppend to an existing log
/UNILOG:fileUnicode log, for non-ASCII file names
/TEEPrint to the console as well as the log
/NPNo per-file percentage counter
/NFL / /NDLLeave file names or folder names out of the log
/FPLog full paths
/TSLog source timestamps
/VVerbose, also lists skipped files
/XReport all extra files, not only selected ones
/BYTESSizes in bytes instead of k, m, g
/NJH / /NJSNo job header or no job summary
/ETAShow the estimated finish time per file
/SAVE:nameSave the options to a job file
/JOB:nameRun with the options from a saved job file
/QUITStop 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:

  • /COPYALL for data, attributes, timestamps, ACLs, owner and auditing
  • /B to read files whose ACL locks out even administrators
  • /DCOPY:DAT for folder timestamps
  • /R:1 /W:1 so 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.

SituationRetry 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 /IORATE to throttle instead)
  • /EFSRAW for EFS-encrypted files
  • /LFSM low 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:

CodeMeaningTreat as
0Nothing copied, source and destination already matchSuccess
1Files copiedSuccess
2Extra files in the destination, nothing copiedSuccess
3Files copied, extra files present (1+2)Success
4Mismatched files or folders detectedSuccess, check log
5Files copied, some mismatched (1+4)Success, check log
6Extra and mismatched, nothing copied (2+4)Success, check log
7Copied, mismatched and extra (1+2+4)Success, check log
8Some files failed after all retriesFailure
9 to 15A failure plus any of the bits aboveFailure
16Serious 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.

Keep reading

Claude Code Cheat Sheet: Commands, Shortcuts and Flags (PDF) AI Claude Code Cheat Sheet: Commands, Shortcuts and Flags (PDF) OpenAI Codex CLI Cheat Sheet: Shortcuts, Commands, Flags (PDF) AI OpenAI Codex CLI Cheat Sheet: Shortcuts, Commands, Flags (PDF) Linux and DevOps Cheat Sheets (Free PDF Pack) Cheat Sheets Linux and DevOps Cheat Sheets (Free PDF Pack) Install Grok Build on Windows 11 (Formerly Cursor) AI Install Grok Build on Windows 11 (Formerly Cursor) netsh Commands Cheat Sheet: Wi-Fi, Firewall, Port Proxy (PDF) Cheat Sheets netsh Commands Cheat Sheet: Wi-Fi, Firewall, Port Proxy (PDF) Install Applications from Windows Command Line Interface Windows Install Applications from Windows Command Line Interface

Leave a Comment

Press ESC to close