I have been doing a check like this to check last modified date:
if($file1.LastWriteTime -gt $file2.LastWriteTime) { }
How can I do something similar but compare if the files are equal. Note that these files are always just text files.
I have been doing a check like this to check last modified date:
if($file1.LastWriteTime -gt $file2.LastWriteTime) { }
How can I do something similar but compare if the files are equal. Note that these files are always just text files.
You can compare file texts as strings. To do that, first get each file as a single string, then compare them for equality.
$filetext1=[System.IO.File]::ReadAllText("file1.txt")
$filetext2=[System.IO.File]::ReadAllText("file2.txt")
$equal = $filetext1 -ceq $filetext2 # case sensitive comparison
Use like below, fastest and shortest way to compare all type of files:
$(Get-FileHash $file1).hash -eq $(Get-FileHash $file2).hash
You could do something like
$file1 = get-content file1 -raw
$file2 = get-content file2 -raw
Compare-Object $file1 $file2 -caseSensitive
That will compare the contents of the files instead. This would work fine on smaller files. If you are working with large files consider using md5 checksum instead. Look at the following post for an example.
The main difference between md5 and reading the entire file into memory is that depend ending on the md5 implementation it is not necessary to hold the entire contents of both files in memory to make the comparison.