Powershell File Compare

You're just giving two strings to Compare-Object, you want the file contents:

Compare-Object (gc $File1) (gc $File2)

Also the output you're giving does not mean that the files are the same.


I guess it's going to depend on your definition of "The same"

Compare-Object (ls Source\Test.*) (ls Dest\Test.*) -Property Name, Length, LastWriteTime

That will compare the actual FILE objects by name, length, and modified date. Adding -IncludeEqual will result in also showing ones that are the same in both places.

If you want to only copy files from "Source" which aren't the same in the "Destination" just do this:

Compare-Object (ls $Source) (ls $Destination) -Property Name, Length, LastWriteTime -passthru | 
Where { $_.PSParentPath -eq (gi $Source).PSPath } |
Copy-Item $Destination

DO NOT start writing scripts that get-content (as suggested by others) to do a file comparison -- you'd be loading everything into memory...

DO consider using robocopy (even though it's command-line syntax is dated). =Þ


(Get-FileHash $path1).Hash -eq (Get-FileHash $path2).Hash

What's wrong with comp.exe or fc.exe? Just check the $? after calling them to see if the command succeeded or not.

Tags:

Powershell