Why does Compare-Object work as expected where -EQ fails to accurately compare arrays of strings?
I had a PowerShell script which was populating an array of strings and was using the -EQ operator to test against the expected values - this was always failing - I think the following code illustrates the issues
# Setting up 4 Lists - $Lists1 2 and 3 should be identical and $List4 differs
[string[]]$List1 = "AA","BBB"
$List2 = $List1
[string[]]$List3 = "AA"
$List3 += "BBB"
[string[]]$List4 = $List3
$List4 += "CCCC"
"--------"
"Checking for Equality of the Lists using the -EQ comparison operator (why do all fail--- when only List4 should fail)"
"--------"
if ($List1 -eq $List1) {"List 1 and 1 are equal"} else {"List 1 and 1 are NOT equal"}
if ($List1 -eq $List2) {"List 1 and 2 are equal"} else {"List 1 and 2 are NOT equal"}
if ($List1 -eq $List3) {"List 1 and 3 are equal"} else {"List 1 and 3 are NOT equal"}
if ($List1 -eq $List4) {"List 1 and 4 are equal"} else {"List 1 and 4 are NOT equal"}
""
""
"--------"
"Checking using Compare-object (operates as expected - only List4 Differs)"
"--------"
if ((compare-object $List1 $List1) -eq $null) {"List 1 and 1 are equal"} else {"List 1 and 1 are NOT equal"}
if ((compare-object $List1 $List2) -eq $null) {"List 1 and 2 are equal"} else {"List 1 and 2 are NOT equal"}
if ((compare-object $List1 $List3) -eq $null) {"List 1 and 3 are equal"} else {"List 1 and 3 are NOT equal"}
if ((compare-object $List1 $List4) -eq $null) {"List 1 and 4 are equal"} else {"List 1 and 4 are NOT equal"}