Compare objects in an if statement Powershell Compare objects in an if statement Powershell powershell powershell

Compare objects in an if statement Powershell


Get-Content returns an array of strings. In PowerShell (and .NET) .Equals() on an array is doing a reference comparison i.e. is this the same exact array instance. An easy way to do what you want if the files aren't too large is to read the file contents as a string e.g.:

$old = Get-Content .\Old.txt -raw$new = Get-Content .\Newt.txt -rawif ($old -ceq $new) {    Write-Host "They are the same"}

Note the use of -ceq here to do a case-sensitive comparison between strings. -eq does a case-insensitive compare. If the files are large then use the new Get-FileHash command e.g.:

$old = Get-FileHash .\Old.txt$new = Get-FileHash .\New.txtif ($old.hash -eq $new.hash) {    Write-Host "They are the same"}