Author Posts

April 7, 2016 at 8:14 am

Why doesn't this work as expected? Does it have anything to do with the extra PSComputerName property?

I'm trying to compare the contents of 2 folders after a DFS replication that didn't quite complete as it should have, so I'm left with a source and a dest that are different. I need to detect the differences and copy the files/directories which were left behind. Oh, and there are over 160,000 files under this folder, so I need it to be quick. Hence the Invoke-Command, rather than simply running GCI against the UNC path. It's not over a super slow link, but it's not gigabit speed either, so efficiency is paramount when detecting differences.

$source = Invoke-Command -computername remotecomputer -scriptblock {Get-ChildItem -Recurse -path "c:\users\user\desktop\folder"}
$dest = Get-ChildItem -Recurse -path "c:\users\user\desktop\folder"
Compare-Object -ReferenceObject $source -DifferenceObject $dest

April 7, 2016 at 10:53 am

What exactly doesn't work? On my testlab the code works as you wrote it.

April 7, 2016 at 12:38 pm

The code executes, yes, but despite both directories having identical file contents, Compare-Object returns 100% differences.

April 7, 2016 at 1:01 pm

And what if you try this? It the only will return the name of the files, and not the whole object for every file.

$source = Invoke-Command -computername server01 -scriptblock {Get-ChildItem -Name -Recurse -path 'c:\temp'}
$dest = Get-ChildItem -Name -Recurse -path 'c:\temp'
Compare-Object -ReferenceObject $source -DifferenceObject $dest

April 7, 2016 at 1:04 pm

I suppose I could do that, but then it wouldn't compare any other data, such as length. Nice short-term fix, though. I am wondering if anyone here can tell me exactly why this is working the way that it is – and if it's just a limitation of using Invoke-Command to run a Get-ChildItem and compare the differences with a locally run GCI.

April 7, 2016 at 1:35 pm

Compare-Object does exactly what the cmdlet name suggests; it compares object. It will compare ALL the properties of the objects that are being compared. A better way to use Compare-Object as opposed how I was suggesting, is actually specifying the property you want to compare on, like this:

$source = Invoke-Command -computername server01 -scriptblock {Get-ChildItem -Recurse -path 'c:\temp'}
$dest = Get-ChildItem -Recurse -path 'C:\Temp'
Compare-Object -ReferenceObject $source -DifferenceObject $dest -Property PSChildname,LastWriteTime | Sort-Object PSChildName

This will compare the filenames and last write time in both directories. Does this help you a bit?

April 7, 2016 at 2:49 pm

Yeah, it does. Thanks! I guess that means if a Get-Member doesn't match up perfectly, Compare-Object will always show a left or right arrow indicator.