Skip to content

Instantly share code, notes, and snippets.

@farhanjiwani
Last active October 29, 2025 18:36
Show Gist options
  • Select an option

  • Save farhanjiwani/80284698194acdea2d8944b3818b2ea1 to your computer and use it in GitHub Desktop.

Select an option

Save farhanjiwani/80284698194acdea2d8944b3818b2ea1 to your computer and use it in GitHub Desktop.
Robocopy (PowerShell): Copy Entire Drive Excluding System Files
robocopy "S:\" "T:\" /E /ZB /COPYALL /R:1 /W:1 /XJ /XD "$RECYCLE.BIN" "System Volume Information" /XF "hiberfil.sys" "pagefile.sys" /LOG:robocopy_log.txt /TEE

Robocopy Command (PowerShell)

Explanation of switches

  • "S:\" "T:\": The source and target directories. The quotation marks are important for paths with spaces, and using them here is a good practice.
  • /E: Copies all subdirectories, including empty ones.
  • /ZB: Uses restartable mode. If access is denied, it switches to Backup mode to handle restricted files, but some system files are inherently locked.
  • /COPYALL: Copies all file information, including data, attributes, timestamps, NTFS access control lists (ACLs), owner information, and auditing information.
  • /R:1: Retries failed copies 1 time.
  • /W:1: Waits 1 second between retries.
  • /XJ: Excludes junction points, which can prevent issues with infinite loops caused by file system links.
  • /XD "$RECYCLE.BIN" "System Volume Information": Excludes the Recycle Bin and the System Volume Information folders. These are internal Windows directories and do not need to be copied.
  • /XF "hiberfil.sys" "pagefile.sys": Excludes the hibernation and virtual memory files. These files are active system processes and cannot be copied while Windows is running.
  • /LOG:robocopy_log.txt: Creates a log file named robocopy_log.txt. This is helpful for reviewing what was copied and any files that were skipped due to errors.
  • /TEE: Outputs the log to both the console window and the log file.

Extra Notes

  • Run as administrator: The command must be executed from an elevated prompt.
  • Review the log file: After the copy is complete, examine the log file for any errors or skipped items. This is especially important for copying a live OS drive, as some files will always be in use.
  • Alternative for a system drive: For a perfect, bootable copy of your OS drive, Robocopy is not the right tool. You would need specialized disk-cloning software to create a sector-by-sector copy, including the boot sector and other system-level data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment