Using Robocopy to Copy Stuff

by Dec 2, 2010

You should avoid cmdlets if you need to copy large files or a large number of files. Instead, you should use native commands like robocopy. This script collects all log files three levels deep into your Windows folder and copies them to some other folder.

robocopy $env:windir\ c:\logfiles\ *.log /R:0 /LEV:3 /S /XD *winsxs* |
Where-Object { $_} |
Foreach-Object {
Write-Progress "Robocopy is looking for LOG-files in:" $_.Split([char]9)[1]
$files = dir c:\logfiles *.log -Recurse
$files | Move-Item -Destination c:\logfiles\ -Force
dir c:\logfiles | Where-Object { $_.psiscontainer } | del -Recurse -Force

Robocopy is fast, robust, and highly configurable. Take, for example, the above code where the tool only traverses three levels deep into the Windows sub-folders and omits the winsxs-subfolder. Although robocopy is a native command, you can see that PowerShell’s pipeline can capture and display its feedback messages in real time. It is part of Windows Server 2008 and Windows 7 and can also be downloaded separately as part of the resource kit.

Twitter This Tip!
ReTweet this Tip!