ROBOCOPY command in Windows Command

XCOPY can copy files. Robocopy can copy files the way a sysadmin actually needs them copied. Built into every version of Windows since Vista, Robocopy handles network interruptions, preserves NTFS permissions, filters by file attributes, runs multi-threaded, and logs everything — all from a single command. It is the default choice for any file operation that matters.

Most admins know the basics, but Robocopy has a handful of behaviors that are easy to misread — including an exit code system where a non-zero result does not always mean failure. This article covers the practical patterns: what flags actually change behavior, where the defaults will surprise you, and when to reach for something else.


Quick answer

Copy a folder and all its contents, preserving structure:

robocopy C:\Source D:\Destination /E /COPYALL /R:3 /W:5

What it does

Robocopy (Robust File Copy) is a command-line file replication utility. Unlike copy or xcopy, it was designed for production use: it retries on failure, restarts interrupted transfers, skips files that are already up to date, and can run multiple copy threads simultaneously.

Basic syntax:

robocopy SOURCE DESTINATION [FILE [FILE ...]] [OPTIONS]

Key parameter groups:

Parameter groupExamplesPurpose
Copy options/E, /S, /MIRWhat to copy and how deep
File attributes/COPYALL, /COPY:DATWhich file properties to preserve
Retry behavior/R:N, /W:NRetries on locked/busy files
Performance/MT:NNumber of copy threads (default: 8)
Logging/LOG, /TEE, /NPOutput and log file control
Filtering/XF, /XD, /XAExclude files, folders, or attributes
Note: Robocopy is available on all Windows versions from Vista onward and on Windows Server 2003 R2 and later. No installation required — it ships as part of the OS.

Practical examples

1. Copy a folder tree with full attribute preservation

The problem: You need to move a folder from one server to another and preserve NTFS permissions, timestamps, and all file attributes — not just the file data. A standard copy or drag-and-drop operation in Explorer strips permissions and resets timestamps.

The solution: Use /E to include all subdirectories (including empty ones) and /COPYALL to preserve every file property Robocopy can carry.

rem Copy D:\Data to E:\Backup\Data with full attribute preservation
rem /E       — copy all subdirectories, including empty ones
rem /COPYALL — copy Data, Attributes, Timestamps, Security (ACLs), Owner, Auditing
rem /R:3     — retry 3 times on locked files before skipping
rem /W:5     — wait 5 seconds between retries

robocopy D:\Data E:\Backup\Data /E /COPYALL /R:3 /W:5
Warning: /COPYALL copies ACLs and ownership. If the destination server is in a different domain or has different local accounts, the copied permissions may reference SIDs that do not exist on the target — which can silently deny access. Use /COPY:DAT instead when copying across domains or to a standalone server.

2. Mirror a directory — keep destination in sync

The problem: You maintain a folder that is updated regularly — scripts, configs, shared resources — and you want the destination to always be an exact copy of the source, including removal of files that were deleted from the source.

The solution: /MIR mirrors the source to the destination: it copies new and changed files, and deletes anything in the destination that no longer exists in the source.

rem Mirror C:\Scripts to \\SRV-FILE-01\Scripts$
rem /MIR  — mirror mode: copies new/changed files AND deletes files removed from source
rem /R:2  — retry twice on busy files
rem /W:10 — wait 10 seconds between retries (UNC path may be slow)

robocopy C:\Scripts \\SRV-FILE-01\Scripts$ /MIR /R:2 /W:10
Common mistake: Running /MIR with source and destination swapped. Robocopy will delete everything in what you intended to be the source. Double-check the argument order before running mirror operations. There is no undo — deleted files bypass the Recycle Bin.

3. Copy over a slow or unreliable network connection

The problem: You are copying large files to a remote site over a VPN or WAN link. The connection drops mid-transfer, and when you restart, the whole file begins again from zero — wasting time and bandwidth.

The solution: /Z enables restartable mode. If the connection drops, Robocopy picks up where it left off rather than restarting the file from the beginning.

rem Copy to remote site with resume support
rem /Z    — restartable mode: resumes interrupted file transfers
rem /E    — include all subdirectories
rem /R:10 — retry 10 times (more retries for unreliable WAN links)
rem /W:30 — wait 30 seconds between retries (give the link time to recover)
rem /NP   — suppress progress percentage output (cleaner on slow connections)

robocopy D:\Transfer \\REMOTE-SRV-01\Transfer$ /Z /E /R:10 /W:30 /NP
Note: Restartable mode (/Z) is slower than normal mode because Robocopy writes a checkpoint after each block. For local or fast LAN copies where drops are unlikely, omit /Z for better throughput.

4. Log the copy operation to a file

The problem: You are running a large copy as part of a migration or scheduled task. You need a record of what was copied, what was skipped, and whether any files failed — especially if the job runs overnight unattended.

The solution: /LOG writes all output to a file. /TEE also prints to the console at the same time so you can watch progress while it runs.

rem Copy with full logging
rem /E              — include all subdirectories
rem /COPYALL        — preserve all file attributes
rem /R:3 /W:5       — retry behavior
rem /LOG:C:\bat\robocopy-log.txt — write output to log file (overwrites each run)
rem /TEE            — also print output to console while writing to log

robocopy D:\Data E:\Backup\Data /E /COPYALL /R:3 /W:5 /LOG:C:\bat\robocopy-log.txt /TEE
Note: Use /LOG+:filename (with a plus sign) to append to an existing log file instead of overwriting it. Useful when running Robocopy from a scheduled task and you want to accumulate a history across multiple runs.

5. Speed up large local copies with multiple threads

The problem: You are copying thousands of small files between two local disks. The transfer is slow even though disk utilization is low — the bottleneck is Robocopy queuing one file at a time rather than the disk itself.

The solution: /MT:N runs N copy threads in parallel. The default is 8. For local disk-to-disk copies with many small files, increasing this can significantly reduce total copy time.

rem Multi-threaded copy for large numbers of small files
rem /E     — include all subdirectories
rem /MT:16 — run 16 parallel copy threads (default is 8)
rem /NP    — suppress per-file progress output (reduces console noise with many threads)
rem /NDL   — suppress directory listing output (cleaner log with /MT)

robocopy C:\AppData D:\AppData-Backup /E /MT:16 /NP /NDL
Warning: /MT is incompatible with /IPG (inter-packet gap) and may produce unreliable results when combined with /Z (restartable mode). For network copies where drops are a concern, use single-threaded mode with /Z instead.

Hidden gems

Exit code 1 is not an error

Robocopy uses a bitmask exit code system, not a simple pass/fail. Exit code 0 means nothing was copied (files were identical). Exit code 1 means files were copied successfully. Exit codes 2–7 are combinations of informational flags. Only exit codes 8 and above indicate an actual error. This causes problems in Task Scheduler and scripts that treat any non-zero exit code as failure — a completed Robocopy job gets flagged as failed. The fix: add exit /b 0 at the end of your batch script, or check for %ERRORLEVEL% GEQ 8 rather than NEQ 0.

Common mistake: Using IF %ERRORLEVEL% NEQ 0 GOTO error after Robocopy in a batch script. A successful copy returns exit code 1, which triggers the error branch. Use IF %ERRORLEVEL% GEQ 8 GOTO error instead.

Exclude specific folders or files with /XD and /XF

You can exclude entire directories with /XD foldername or specific files with /XF pattern. Both accept wildcards and multiple values in a single flag. This is useful when mirroring a folder that contains temp files, cache directories, or logs you do not want replicated.

rem Mirror source excluding temp folders and log files
rem /XD — exclude directories matching these names (anywhere in the tree)
rem /XF — exclude files matching these patterns

robocopy C:\AppRoot D:\AppRoot-Mirror /MIR /XD Temp Cache .git /XF *.log *.tmp

/L — dry run without copying anything

Adding /L to any Robocopy command makes it list what it would do without actually doing it. No files are copied, no directories are created, nothing is deleted. This is the safest way to validate a /MIR command before running it against a production share — you can see exactly which files would be added, updated, and deleted.

rem Dry run — shows what /MIR would do without making any changes
robocopy C:\Scripts \\SRV-FILE-01\Scripts$ /MIR /L
Result: Output shows every file that would be copied, skipped, or deleted — prefixed with [would copy], [would skip], [would remove]. No changes are made to source or destination.

PowerShell equivalent

PowerShell’s Copy-Item can copy files and folders recursively, but it does not have built-in retry logic, restartable mode, multi-threading, or ACL-aware copying without extra work. For simple copies it is fine:

Copy-Item -Path "C:\Source" -Destination "D:\Destination" -Recurse -Force

For anything more complex, calling Robocopy from PowerShell is the standard approach — it is not a workaround, it is the intended pattern:

# Call Robocopy from PowerShell — full flag support, capture exit code
robocopy C:\Source D:\Destination /E /COPYALL /R:3 /W:5 /LOG:C:\bat\copy-log.txt

# Check exit code — 8 or above means actual errors occurred
if ($LASTEXITCODE -ge 8) {
    Write-Error "Robocopy reported errors. Check C:\bat\copy-log.txt"
}
Note: In PowerShell, Robocopy exit codes are available via $LASTEXITCODE. Use -ge 8 to check for real errors — same logic as in batch scripts.

Where this matters

Server migrations — moving file shares from one server to another with permissions intact, without a domain-level DFS migration tool.

Scheduled backups — running nightly incremental copies via Task Scheduler where only changed files are transferred and the result is logged automatically.

Remote site sync — keeping a folder in sync over a WAN link where the connection may drop and restartable mode prevents wasted bandwidth.

Build artifact distribution — copying compiled outputs or deployment packages from a build server to multiple targets in parallel.

Pre-migration staging — running a first full copy days before a cutover, then running again on cutover day to transfer only the delta — dramatically reducing downtime.


Tips and limitations

  • Default retry count is 1,000,000. If you do not set /R:N, Robocopy retries a locked file one million times with a 30-second wait between attempts. A single locked file can stall a job for days. Always set /R:3 /W:5 or similar for any unattended copy.
  • /MIR deletes files in the destination. It is not a one-way add-only sync — it actively removes files from the destination that are not present in the source. Always run with /L first if you are unsure.
  • Paths longer than 256 characters. Use the UNC prefix \\?\ for paths that exceed the MAX_PATH limit: robocopy \\?\C:\LongPath \\?\D:\Destination /E.
  • Open files are skipped, not failed. Files locked by another process are skipped after the retry limit. They appear in the log as skipped — not as errors. Check the skip count in the summary output after large migrations.
  • /COPYALL requires elevation. Copying security descriptors (ACLs) requires administrator privileges. Running Robocopy without elevation and with /COPYALL will silently fall back to copying data only.

Official documentation

Related tools

Related guides

  • ATTRIB command — manage file attributes that affect which files Robocopy copies or skips
  • DIR command — list and inspect files before and after a Robocopy operation
  • DISKPART command guide — prepare destination disks before large Robocopy migrations
  • WMIC vs PowerShell — when to use legacy CLI tools vs modern alternatives in the same scripting context