Powershell Mirror Directories

A Custom Back-up Solution for external drives

powerShellIconRecently I re-installed windows and re-initiated my development environment. However, when it came to finding a back-up solution for data files (that is, no-system user-files) I was not satisfied with my options: Windows back-up does not support non-windows drives and external hard-drives, and the free-software out there isn’t what I’m looking for. Perhaps there’s a good software option out there, but I didn’t find what I was looking for.

I want my back-up tool to do the following:

  • Back-up the specified directories when I want it to
  • Be efficient. I.e. work quickly
  • Be aware of back-up location. I.e. external hard-drives where the drive-letter might change.
  • Little-to no overhead and extra bes

The closest thing I came to, is a PowerShell script by Mathew Painter. His excellent article and script can be found here.

Mathew’s script is excellent: it checks to ensure that the drive-letter of the external drive did not change, if it did change, it updates the back-up destination (if it still exists), and with robocopy it runs very efficiently and quickly. However, the solution lacked the first pre-requisite as it only backed-up a whole drive. For me, this is unnecessary as I’m only concerned with 2 folders on my D: drive so I endeavored to re-fashion his script.

Essentially the script mirrors a directory of your choosing into a directory of your choosing. Sounds simple, right? It is, but it’s not.

  • You could just drag and drop your files, but that’s something you cannot automate. Plus it’s not as fast as robocopy and it doesn’t truly mirror the files. I.e. directories and files that were deleted in the source location will not be deleted in the destination (back-up) location.
  • You could just use robocopy with the appropriate arguments, but then what if the letters of the external hard-drives change (when re-plugging them in a different order).

Running the Script

Here’s how this script works. When you run it for a first time, it will ask you the input details:

  1. Source Drive letter. E.g. If you are copying something for the C:\ drive, type in “C”. NOTE, do not type anything in but the letter (Not “C:” or “C:\”.
  2. Source path. E.g. If you are copying “C:\Users\username\Documents”, type in, “Users\dardawk\Documents”. Note that you need to drop the drive letter, collon and backslash.
  3. Destination letter: E.g. “F:\” would be “F”
  4. Destination path: A pre-existing path where your directory will be mirrored to. So if you have a “F:\backup” folder, you should type in, “backup” here.

An example of backing up D:\testMirror to J:\backup-mirror\testMirror:


And that’s it.

The script will generate a mirror-directories.xml file that will be used to automate the process so that the next time you run the script, no user input will be necessary (except to click continue). As well, robocopy will generate a report for you that will be saved in the source directory.


Since I customized this script so that it works on directories, it may be necessary to have multiple copies of the script (with multiple set-up xml files). I added in this feature so that if you have multiple copies of the script with different names in the same directory (let’s say backup1.ps1, backup2.ps1, backup3.ps1, …) the script will create the following setup set-up xml files after running the first time: backup1.xml, backup2.xml, backup3.xml.

Simple as that.

After you run the backup for the first time, you can set-up run it regularly through Windows Scheduler.


A warning: This script is provided as-is, and it’s recommended that only power-users utilize it. Personally, I would never run a powerShell script which code I didn’t read through at least once.

People who should not download this script:

  • If you don’t know what a PowerShell is
  • If you don’t know how to enable unblock a PowerShell script that’s downloaded from the internet (For this reason, I did not illustrate how to do this in this post)
  • If you don’t know how to change the PowerShell execution policy. (For this reason, I did not illustrate how to do this in this post)

Having said that, here is the download link:

Download Link

Reference Links

Leave a Reply

Your email address will not be published. Required fields are marked *