Robocopy and PowerShell working in harmony.

 

So lets create a scenario,  and say that they were having difficulty using Robocopy commands within or with PowerShell. Here’s a demonstration of how to use PowerShell to be able to deliver seamless and accurate commands that will ensure that you will have your data copied and have your PowerShell script.

The best part of PowerShell is the ability to manipulate objects of different types in the same way regardless of data type.

let’s start with planning out the end product and work towards constructing our objects.

 

Import your file with source and destination or you can wing it into a function. For demonstration purposes we will cover both.

 

Example 1: Scripted with external file input 

$data=import-csv data.csv

$count=0

$total=$data.count

foreach($item in $data)

{

$count+=1 #some people use $count++

$source= $item.source

$destination=$item.Destination

Write-Progress -Activity “Copying $source to $destination” -PercentComplete ($count/$total*100)

Robocopy $source $destination /mir /mt:8 /r:1 /w:1

} #end foreach statement in script

 

This one s a little tricky due to the possibility of quotation exceptions;  Folders with spaces that require quotes.

 Example 2: Interactive PowerShell session

set-location c:\rootoftargetfolders

get-childitem  |foreach-object {
$source= $_.fullname
$destination=”d:\destinationfolder\$($_.basename)”
Robocopy “$source” “$destination” /mir /mt:8 /r:1 /w:1
} #end foreach-object

 

Example 3: Function best saved in $profile (PowerShell Profile) 

function RoboCopy-Item ($Source,$destination)

{

Robocopy $Source” “$Destination /mir /mt:8 /r:1 /w:1

} #end function

#now to try it

RoboCopy-item  -Source c:\files -destination D:\usb_documents

 

Conclusion:

I know sometimes dealing with syntax can be a headache but when you take the time to experiment with the shell it is a good path to accumulate lessons learned.

RoboCopy and PowerShell can play nice, of course adding in some try/catch to control your logging output to a text, html or csv or installing a module that can output logs directly to Microsoft Excel or take the old-school approach and use the /log file switch for RoboCopy.

 

 

Published by Xajuan Smith

Computer Information Specialist, with a desire to make the masses feel what I was born to channel...The emotion that strives us to do more, speak more boldly and with most sincere ideals that will make life easier and will not hinder the dreams we all have at any given time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: