Copying a set of Folders via Powershell and Robocopy

September 22, 2011

I wanted to move some files from one location to another. All of the folders started with a set prefix. For example, “Backup One”, “Backup Two”, “Backup 2011-09″, etc. I wanted to use Robocopy because of it’s “restartable” mode. I’m still quite new to Powershell so I figure I better blog this one as I’m sure I’ll need it, or something similar, again.

In my sample below, “ParentFolder” is the parent folder (duh) of the “Backup” folders above. Therefore, we have something like this:

D:\ParentFolder\Backup One
D:\ParentFolder\Backup Two
D:\ParentFolder\Backup 2011-09

The command:

Get-ChildItem D:\ParentFolder -Filter "Backup*" -Name | ForEach-Object { robocopy "D:\ParentFolder\$_" "X:\$_" /Z /S /MOVE }
  • -Filter parameter only returns those items that start with “Backup”. This will probably return files too. I didn’t have them, so it wasn’t a problem, but I should probably figure that out too.
  • -Name parameter only shows the folder name, nothing else.
  • Iterate over each item returned and perform the robocopy command, moving all the files and subfolders to X:.

After the command runs, you should end up with, along with all of the files within, of course:

X:\Backup One
X:\Backup Two
X:\Backup 2011-09

tags: , ,
posted in Powershell by Sherman

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org