PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Geek Sync Live

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.


As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Using Friendly Robocopy

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!

Copyright 2012 PowerShell.com. All rights reserved.