I have found something unusual with PowerShell strings...I hope this is just something I'm missing. Here's what I found.
I copied some data from a web page (KQL searches for O365 if anyone cares), and it contained hidden control characters. Here are the two lines as I stored them in PowerShell variables:
PS C:\Users> $L1 = "(c:c)(date=01/01/2014..11/01/2016)"
PS C:\Users> $L2 = "(c:c)(date=01/01/2014..11/01/2016)"
The second line has two hidden characters (Unicode character 8206) between the "(c:c)" and the "(date". I believe they are still there in this post. Because of this, the two strings have different lengths:
PS C:\Users> $L1.Length
34
PS C:\Users> $L2.Length
36
And yet, when I check to see if they are equal to each other, PowerShell tells me they are:
PS C:\Users> $L1 -eq $L2
True
Now, I know it understands that there are differences there. I can use the substring method to pull out some characters, and show that the substrings are not equal:
PS C:\Users> $L1.Substring(4,8)
)(date=0
PS C:\Users> $L2.Substring(4,8)
)(date
If I use the -eq operator to compare those substrings, I get the correct answer:
PS C:\Users> $L1.Substring(4,8) -eq $L2.Substring(4,8)
False
But if I force the strings to display the same by changing how many characters I grab so I get the same 6 visible characters, then PowerShell will tell me that they are equal:
PS C:\Users> $L1.Substring(4,6)
)(date
PS C:\Users> $L2.Substring(4,8)
)(date
PS C:\Users> $L1.Substring(4,6) -eq $L2.Substring(4,8)
True
So, is this expected behavior? Am I missing something? I've been programming long enough that I know string handling can be weird...but this is beyond my personal experience.
If it is expected behavior, how to I properly compare two string variables to see if they are exactly the same?