trio.so
How-Tos

How to Back Up Files on Windows 10 and 11 Using Command Prompt

Back up files on Windows 10 and 11 from Command Prompt with robocopy, xcopy and wbadmin, plus a ready-to-use backup script you can schedule.

Mountain landscape representing leadership perspective and vision
Written by
Atena Ladchartabi

Content writer, trio.so

Published on
Modified on

The quickest way to back up files from Command Prompt is robocopy. Open Command Prompt and run robocopy C:\Data D:\Backup /E /R:2 /W:5 /LOG:D:\Backup.log. It copies the folder and every subfolder, retries a failed file twice, and writes a log. Use xcopy for a simple one-off copy and wbadmin for a system or volume backup. All three work on Windows 10 and Windows 11.

Which command to use

CommandUse it forWatch out for
robocopyFolder backups you repeat. Retries, logging, only changed files./MIR deletes files at the destination.
xcopyA quick copy of a folder tree.No log file and no retry control.
wbadminVolume and system backups to a drive or network share.Needs a Command Prompt run as administrator, and an account in Administrators or Backup Operators.

None of these is a backup by itself until a copy lives on a different disk or machine. Back up to an external drive or a network share, never to a second folder on the same disk.

Back up files with robocopy

Robocopy ships with Windows 10 and 11. Syntax: robocopy <source> <destination> [options].

robocopy C:\Data D:\Backup\Data /E /R:2 /W:5 /LOG:D:\Backup\backup.log
  • /E copies all subfolders, including empty ones.
  • /R:2 /W:5 retries a failed file twice and waits 5 seconds between tries. The default is a million retries at 30 seconds each, which can make a script look hung on a locked file.
  • /LOG:file writes the result to a file. Use /LOG+:file to append.
  • /MT:8 copies with 8 threads for speed on many small files. Use it with a log file.
  • /NP hides the per-file progress percentage so the log stays short.
  • /XD folder skips a folder, for example a temp folder.
  • /Z makes copies restartable, which helps over a slow network share.

By default robocopy skips files that are the same in both places, so the second run only copies what changed.

Mirror a folder, and the warning that goes with it

/MIR mirrors the source to the destination. It is /E plus /PURGE, which deletes files in the destination that no longer exist in the source. If you delete a file from the source by accident, the next mirror run deletes your backup copy too. If you use /MIR, run it once with /L first. That lists what would happen without copying or deleting anything.

robocopy C:\Data D:\Backup\Data /MIR /L /R:2 /W:5

Read the exit code

Robocopy returns a code, not just success or fail. Per Microsoft, 0 means nothing needed copying, 1 means files were copied, and any value of 8 or higher means at least one failure. A script should treat 0 to 7 as success and 8 or above as an error.

Back up files with xcopy

Xcopy is older and has fewer options, but it is fine for a quick copy of a folder tree:

xcopy C:\Data D:\Backup\Data /S /E /H /I /Y
  • /S /E copies subfolders, including empty ones.
  • /H includes hidden and system files.
  • /I treats the destination as a folder if it does not exist.
  • /Y overwrites without asking.
  • /D with no date copies only files newer than the ones already in the destination.

Microsoft notes that xcopy can fail with an "insufficient memory" error on paths longer than 255 characters. Robocopy does not have that limit by default.

Back up volumes and the system with wbadmin

wbadmin is the command line for Windows backup. Open Command Prompt as administrator (right-click, Run as administrator). Your account must be in Administrators or Backup Operators.

wbadmin start backup -backupTarget:E: -include:C: -allCritical -quiet
  • -backupTarget is the drive letter or network share (\\server\share) to write to.
  • -include lists the volumes, folders or files to back up.
  • -allCritical adds every volume the operating system needs, for a bare metal restore. The target cannot be one of the volumes in the backup.
  • -quiet runs without prompts, which a scheduled job needs.

Two cautions from Microsoft's documentation. A second backup to the same network share overwrites the first, so use a subfolder per run. And a failed run can leave you with an unusable newest backup and no older one. For everyday file backups, robocopy is easier to read and recover from.

A ready-to-use backup script

Save this as C:\Scripts\backup.ps1. It copies one folder, writes a dated log and exits with an error code if robocopy reports a failure.

$src = "C:\Data"
$dst = "D:\Backup\Data"
$log = "D:\Backup\logs\backup-$(Get-Date -Format yyyy-MM-dd).log"

New-Item -ItemType Directory -Force -Path (Split-Path $log) | Out-Null

robocopy $src $dst /E /R:2 /W:5 /MT:8 /NP /LOG:$log

if ($LASTEXITCODE -ge 8) { exit 1 } else { exit 0 }

Set the source and destination, run it once by hand, and open the log to confirm the file count.

Schedule the backup with Task Scheduler

From a Command Prompt run as administrator, this creates a task that runs the script every night at 2:00 as the SYSTEM account:

schtasks /create /tn "Nightly File Backup" /tr "powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\Scripts\backup.ps1" /sc daily /st 02:00 /ru SYSTEM /rl HIGHEST

Run it once on demand with schtasks /run /tn "Nightly File Backup", then check the log. If the destination is a network share, SYSTEM uses the computer's identity on the share, so give the computer account write access or run the task under a service account.

Before you rely on any backup, restore a few files from it. A backup you have never restored is a guess. For restore points on the system drive, see how to enable System Protection from Command Prompt. To stop heavy apps from slowing the machine while it runs, see how to disable startup programs from Command Prompt.

Managing this across many devices

A script on one PC is a chore. On fifty it is a project. trio.so runs scripts on enrolled Windows PCs from the console, so you can write the robocopy backup script once and run it on the machines that need it. See remote terminal and script runs.

The trio.so Create script wizard with a PowerShell robocopy script that backs up a Documents folder to a network share.
In trio.so, IT writes one robocopy backup script and runs it on enrolled Windows PCs.

Ready-to-use Templates

Must-have Template Toolkit for IT Admins

Explore All
Template Toolkit

Run a backup script on many PCs

trio.so runs scripts on enrolled Windows PCs from the console.

Start free trial

Manage Every Device From One Console

trio.so brings device management, patching and security policies for Windows, macOS, Linux, iOS/iPadOS and Android into one console. Enroll a device and see it in the console within minutes.

14-day free trial. No credit card required.

trio.so brings device management, patching and security policies for Windows, macOS, Linux, iOS/iPadOS and Android into one console. Enroll a device and see it in the console within minutes.

Smiling womanAbstract geometric patternAbstract geometric patternSmiling womanSmiling woman

Frequently Asked Questions (FAQ)

Have questions? We've got answers. This section covers some of the most commonly asked questions related to this topic.

Run robocopy C:\Data D:\Backup /E /R:2 /W:5 /LOG:D:\Backup.log to copy a folder and its subfolders to another drive and log the result.

wbadmin is the Windows command line tool for backups. wbadmin start backup writes volumes, folders or a system image to a drive or network share, and it must run from a Command Prompt opened as administrator.

Use Task Scheduler. schtasks /create with /sc daily /st 02:00 runs your backup script every night at 2:00.

Yes. It removes files in the destination that are not in the source. Run it with /L first to see what it would do.

For repeat backups, yes. Robocopy retries failed files, writes a log, copies only changed files and sets an exit code a script can read.

Related

From the blog

The related industry news, interviews, technologies, and resources.