I'd like to create a PS script that will compare two directories. Then, if one directory should contain a file the other doesn't, I would like that file to be copied to the other. Basically I want to make both directories identical.

This provides an output showing any differences but I would like to have the differences amended by copying the files.

Hopefully I've explained myself clearly. I'm a beginner so please feel free to tell me if this will be too complex for my currently level.

Thanks.

by DonJ at 2012-11-26 07:07:33

So... me, I'd probably just use Robocopy.exe for this ;). It's designed to sync directories and it's a lot faster.

But, if this is just a PowerShell exercise for you, you're on the right track. Assign the results of Compare-Object to a variable, say $result. Then try piping that to Get-Member. You'll see that the difference objects show you which "side" is different, and they contain the different objects.

So in your example, $d1 is left and $d2 is right. I might pass $result to Where-Object, keeping only those items which are on the left and not on the right (what's in $d1 that isn't in $d2). I'd then pipe *those* to Copy-Item or ForEach-Object to copy them from D1 to D2.

by john808 at 2013-04-30 06:24:15

Hi,

I've been working on this again but I'm having problems understanding "where-Object"? The part that is confusing is the syntax....

I'm sure the above is a bit messy and I'm going to continue working on it but thought I would ask for help to see what I get back.

Thanks for any assistance.

by DonJ at 2013-04-30 06:29:08

The output of Compare-Object is a difference object, not your original compared objects. Pipe Compare-Onject to GM and you'll see.

by john808 at 2013-05-01 02:20:03

So, below is my script that does what I wanted, almost. It compares two folders and copies any differences to a third folder. The problem is, when I run it, although the differences are copied to the third folder successfully, I get an error message for every item that is copied? I copied one of the error messages below the script. The error messages are all the same, the only difference is the file name.

I understand why the path can't be found, because as the message state, it does not exist. The actual path of the items is 'C]subfolder[/b]\Useful links.doc'. Where am I confusing my script and myself?

by ArtB0514 at 2013-05-01 05:40:34

One of the biggest problems people have with pipelines is understanding exactly what the $_ object is at any step. To see what you're feeding into the Copy-Item command (and to be able to choose the correct property), try this:

Given that there's only one level of subfolder (and it always exists), you'll probably want to do something like this:Copy-Item "c]Or, you might find that this is what you want:Copy-Item $_.FullName

by john808 at 2013-05-03 03:57:40

Hey art,

I tried your changes but they didn't seem to work. Using "Copy-Item "c]" gave me the same results, the files copy ok, but the same error message is recevied. If I try "Copy-Item $_.FullName" it fails with path errors, adding in a path then states invalid characters?

I've achived what I wanted by copying the files that are different between the two folders,but I was interested to know why I get errors.