Invoking GCI on remote server to compare directories instead of slow UNC

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

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

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

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

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.

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?

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.