Integrate Robocopy in Powershell

PSA

I experiment with all kinds of possibilities regarding old cmd commands and powershell.

If you know Robocopy cmd, you should know it has many switches. Sometimes it's hard to remember it all.

So I put some of the switches in variables and have integrated them in Robocopy's arguments variable like this.

Turns out... it works.

You can add variables as you wish.

I wrote the script so it would create and open a report regarding the copy results.

# Simulate Robocopy "Copy Items" with a log file
$Source = 'D:'
$Destination = "$Env:UserProfile\Documents"
$Name = '*.xml'
$ExcludeOlder = '/XO'
$ExcludeDirectories = '/XD', '*'
$CopyEmptyFolders = '/E'
$NoSystemHiddenfiles = '/A-:SH'
$SimulateOnly = '/L'
$Retry = '/R:0'
$WaitTimeSec = '/W:0'
$MultiThreadedCopies = '/MT'
$Arguments = @($ExcludeOlder, $ExcludeDirectories, $CopyEmptyFolders, $SimulateOnly, $Retry, $WaitTimeSec, $MultiThreadedCopies, $NoSystemHiddenfiles )
$RoboCopy = 'RoboCopy.exe'
$LogPath = "$($Env:UserProfile)\Documents\Robocop.log"
$LogRobocopyReport = "/Log:” + $LogPath
$RoboCopyArgs = @($Source, $Destination, $Name) + $Arguments
Write-Output "Saving your Robocopy Report"
& $RoboCopy @RoboCopyArgs + $LogRobocopyReport
Write-Output "This is your Robocopy Report"
& $LogPath

Posted Aug 15, 2024, 8:02 AM UTC

10
Loading comments…