PowerShell How-To

Using Robocopy To Transfer Files

The GUI is fine if you're only moving a few files, but for larger and more complex migrations, this built-in Windows utility is the way to go.

There are many ways to get a file from Point A to Point B on Windows. You can simply drag and drop with the GUI, use PowerShell (the old-school copy command) and also use a handy tool called robocopy.

When working with a few files here and there, you'll probably choose to go the GUI route. It's easy, you don't have to learn any command-line ninja skills and it gets the job done. However, when you've got a lot of files to transfer and care about things like keeping permissions, copying folders, excluding certain files and more, this is a time when you need to drop down to the command line.

Robocopy (or Robust File Copy) for Windows is a useful utility built into Windows that allows you to efficiently copy or transfer one, 10 or 1,000,0000 files and folders all in one go. Not only can you copy/move files from one place to another, you can also sync folders destinations, too.

To get started with robocopy, open up a Windows command prompt or PowerShell and type robocopy /?. You are soon inundated with all of the options available:

We're not going to get too crazy in this article, though. You are more than welcome to experiment with what's possible using the various switches discovered using the /? switch. For this article, we're going to go over the basics since there is so much you can do with this tool.

We'll start with two folders, C:1 and C:2. Folder1 contains a few text files each labeled 0 through 99.txt. For now, Folder2 is empty. Let's say I want to copy one of these files. I can do that with robocopy using the line robocopy C:1 C:2. This is the simplest way robocopy can be used. Before seeing each file dutifully copy over to Folder2, you'll see a banner. This banner is a summary of the command you just executed.

You can see below that just by specifying a source and destination folder, we actually took a shortcut. Notice Options. This is the actual switches we provided to robocopy without even knowing it. You can see that the switches used were a lot more complicated than just specifying a source and destination folder. However, this is how flexible robocopy can be. We could have just as easily used all of these switches or a combination of them to modify the behavior of robocopy.

In the example below, you can take a look at the robocopy help using the /? switch to see what robocopy did, in detail:

   ROBOCOPY     ::     Robust File Copy for Windows

  Started : Wednesday, January 23, 2019 10:19:22 AM
   Source : C:\Folder1\
     Dest : C:\Folder2\

    Files : *.*

  Options : *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30


At the end of the report, you'll then see a handy summary of what robocopy accomplished.


               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :       100       100         0         0         0         0
   Bytes :         0         0         0         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Wednesday, January 23, 2019 10:19:23 AM

Perhaps I'd rather not copy the entire folder but only certain ones matching specific criteria. With robocopy, that criteria can be just about anything like whether the file exists in the destination folder, if it's newer, if it has different attributes and more. Robocopy has a ton of file selection options to choose from.

:: File Selection Options :
                 /A :: copy only files with the Archive attribute set.
                 /M :: copy only files with the Archive attribute and reset it.
    /IA:[RASHCNETO] :: Include only files with any of the given Attributes set.
    /XA:[RASHCNETO] :: eXclude files with any of the given Attributes set.

 /XF file [file]... :: eXclude Files matching given names/paths/wildcards.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.

                /XC :: eXclude Changed files.
                /XN :: eXclude Newer files.
                /XO :: eXclude Older files.
                /XX :: eXclude eXtra files and directories.
                /XL :: eXclude Lonely files and directories.
                /IS :: Include Same files.
                /IT :: Include Tweaked files.

             /MAX:n :: MAXimum file size - exclude files bigger than n bytes.
             /MIN:n :: MINimum file size - exclude files smaller than n bytes.

          /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
          /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
          /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
          /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                       (If n < 1900="" then="" n="n" days,="" else="" n="YYYYMMDD" date).="" j="" ::="" exclude="" junction="" points="" and="" symbolic="" links.="" (normally="" included="" by="" default).="" ft="" ::="" assume="" fat="" file="" times="" (2-second="" granularity).="" st="" ::="" compensate="" for="" one-hour="" dst="" time="" differences.="" jd="" ::="" exclude="" junction="" points="" and="" symbolic="" links="" for="" directories.="" jf="" ::="" exclude="" symbolic="" links="" for="">

Finally, perhaps you're copying a large amount of data and you're bound to run into some problems. Robocopy can log activity, too. My favorite is the /ETA switch that gives you a bird's-eye view of when those large data copies will be done.

Robocopy is such a powerful tool when performing tasks like big data migrations, keeping two data sources in sync and keeping tabs on the activity using the built-in logging capability. If you have a need for a robust tool to transfer a lot of files, robocopy is a great tool to look to.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.

comments powered by Disqus
Most   Popular