How to compare the contents of two string objects in PowerShell

hax0r_n_code picture hax0r_n_code · Sep 12, 2013 · Viewed 177.2k times · Source

In PowerShell I have an array of string objects, and I have an object that contains string objects. In Java you can do a .equals(aObject) to test if the string values match, whereas doing a == test if the two objects refer to the same location in memory.

How do I run an equivalent .equals(aObject) in powershell?

I'm doing this:

$arrayOfStrings[0].Title -matches $myObject.item(0).Title

These both have the exact same string values, but I get a return value of false. Any suggestions?

Answer

manojlds picture manojlds · Sep 12, 2013

You want to do $arrayOfString[0].Title -eq $myPbiject.item(0).Title

-match is for regex matching ( the second argument is a regex )