Using Friendly Robocopy

by Aug 30, 2016

PowerShell 2+

Robocopy is a tool of choice to copy files, and that does not change with PowerShell. You can, however, use PowerShell to embed robocopy inside a user-friendly PowerShell function. This way, you no longer need to remember the awkward command switches used by Robocopy, and instead use PowerShell parameters and IntelliSense.

A call to robocopy could then look like this:

 
PS C:\> Invoke-Robocopy -Source $env:windir -Destination c:\logs -Filter *.log -Recurse -Open
 

And here is the wrapper:

#requires -Version 3

function Invoke-Robocopy
{
    param
    (
        [String]
        [Parameter(Mandatory)]
        $Source,

        [String]
        [Parameter(Mandatory)]
        $Destination,

        [String]
        $Filter = '*',
        
        [Switch]
        $Recurse,
        
        [Switch]
        $Open
    )

    if ($Recurse)
    {
        $DoRecurse = '/S'
    }
    else
    {
        $DoRecurse = ''
    }

    
    robocopy $Source $Destination $Filter $DoRecurse /R:0 
    
  if ($Open)
  {
      explorer.exe $Destination
  }    
}

Twitter This Tip! ReTweet this Tip!