I'm trying to compare the contents of a text file with a string. Sounds simple, but no luck!
$mystring = @'
hello
goodbye
'@
set-content c:\temp\file.txt $mystring
if (Test-Path "c:\temp\file.txt") {
$myfile = Get-Content "c:\temp\file.txt" -raw
if ($myfile -eq $mystring) {
write-host 'File same'
}
else {
write-host 'File different'
}
}
else {
write-host 'No file'
}
Write-Host $mystring.Length
Write-Host $myfile.Length
Output
File different
14
16
The lengths of the two string are different and when you open the file, there's a new line at the bottom which is where I'm presuming the extra 2 characters in the length are coming from.
What am I missing?