PowerShell -Like vs -Contains Comparison Operators
At some point during your PowerShelling career you will need to test if “something” is found (or not found) within a certain object. I find that I am usually faced with this situation when I am testing if a string “contains” a value or not.
At this point, I am always confused as to which comparison operator to use. From a logical language perspective I always feel like -contains
is the way to go, but then I remember that might not be the correct choice.
To put this issue to bed once and for all (at least for myself), here is a summary of when to use -like
and when to use -contains
.
PowerShell -like Comparison Operator
Here is how Microsoft TechNet describes the use for -like
:
-Like
Description:
Match using the wildcard character (*).Example:
PS C:\> “Windows PowerShell” -like “*shell”
TruePS C:\> “Windows PowerShell”, “Server” -like “*shell”
Windows PowerShell
Here is my version:
-like
is used to compare or find if a string exists within another string.-like
allows you to use the wildcard character (*
) so you can search anywhere within the string.
So back to my initial requirement of determining if a string contains a particular value or not, we would use the -like
operator. See some examples below:
$SearchTerm = 'xyz'
$String = 'abc def ghi jkl mno pqr stu vw xyz'
If ($String -like $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
# Result = False
In example 1, the result is false because there are no wildcards, therefore its checking to see if the string matches exactly.
$SearchTerm = 'xyz*'
$String = 'abc def ghi jkl mno pqr stu vw xyz'
If ($String -like $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
# Result = False
In example 2 we added the wildcard (*
) at the end of the search term. This essentially means check if xyz
exists at the beginning of the string and then ignore the rest. In this case it doesn’t, hence the result is false.
$SearchTerm = 'abc*'
$String = 'abc def ghi jkl mno pqr stu vw xyz'
If ($String -like $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
# Result = True
In example 3 we are doing the same as example 2 but searching for abc*
and therefore returns true because abc
is found at the beginning of the string. The rest is then ignored due to the wildcard.
$SearchTerm = '*xyz'
$String = 'abc def ghi jkl mno pqr stu vw xyz'
If ($String -like $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
# Result = True
Example 4 is essentially a reverse of example 3. We are searching for xyz
at the end of the string and don’t care about anything in front of it (hence the wildcard in the front of the search term).
$SearchTerm = '*mno*'
$String = 'abc def ghi jkl mno pqr stu vw xyz'
If ($String -like $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
# Result = True
Example 5 shows searching for something in the middle of the string by using wildcards on either side of the search term.
PowerShell -contains Comparison Operator
Here is how Microsoft TechNet describes the use for -contains
:
Description:
Containment operator. Tells whether a collection of reference values includes a single test value. Always returns a Boolean value. Returns TRUE only when the test value exactly matches at least one of the reference values.When the test value is a collection, the Contains operator uses reference equality. It returns TRUE only when one of the reference values is the same instance of the test value object.
Syntax:
-ContainsExamples:
PS C:\> “abc”, “def” -Contains “def”
TruePS C:\> “Windows”, “PowerShell” -Contains “Shell”
False #Not an exact match# Does the list of computers in $domainServers
# include $thisComputer?
# ——————————————
PS C:\> $domainServers -Contains $thisComputer
TruePS C:\> “abc”, “def”, “ghi” -Contains “abc”, “def”
FalsePS C:\> $a = “abc”, “def”
PS C:\> “abc”, “def”, “ghi” -Contains $a
False
PS C:\> $a, “ghi” -Contains $a
True
Now here is my version….
The
-contains
comparison operator is used to determine if a collection of objects (e.g. an array) contains a particular object or not. The evaluation is completed against the entire object and not any single object property. In other words, all of the object’s properties are taken into consideration.
That might be a little confusing, but looking at some examples might clear things ups somewhat:
$SearchTerm = 'xyz'
$Collection = 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vw', 'xyz'
If ($Collection -contains $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
#Result = True
The first example is an easy one, an array with each object being a string and therefore makes the comparison really easy. The example above is searching for the array object (array element) of xyz
. As there is an array element that matches xyz
it will return true.
$SearchTerm = 'W32Time'
$Collection = Get-Service
If ($Collection -contains $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
#Result = False
Now you would expect that the example above would return true, because every Windows system has a W32Time
service, but it doesn’t… why? Well because we are attempting to find an object based on a single property, name
. The -contains
operator doesn’t work like this however. It compares every single property of the object and if all of them are the same then it returns True, else it will return false.
In the example above, we are essentially comparing a string object with a System.ServiceProcess.ServiceController
object, which isn’t going to work.
In order to find a service with the name
property we would need to do:
$SearchTerm = 'W32Time'
$Collection = Get-Service
If ($Collection -like $SearchTerm) { Write-Output 'True' } Else { Write-Output 'False' }
#Result = True
Although this works, it might not be the best result because W32Time
might be the value of a property within another service and hence you might get some false positives.
To remove the risk of false positives, I would personally do something like:
$SearchTerm = 'W32Time'
$Collection = Get-Service
ForEach ($Service in $Collection) {
If ($Service.Name -eq $SearchTerm) {
Write-Output 'True'
}
}
#Result = True
Enumerating all services in the collection and determining if each of their name
properties equal the search term is a very accurate way to determine if the particular service exists or not and will eliminate all false positives. More code, but a better solution.
Not Searches?
As a quick side-note, what happens if you want determine if something does not contain a particular search term?
Well in that case just use -NotLike
or -NotContains
in the same fashion and in the same situations as you would use -like
and -contains
.
More Info on Comparison Operators
If you want to learn more about all the comparison operators, then check out the TechNet Comparison Operators page.
Well I hope that helps make things a little clearer on when to use -Like
and when to use -Contains
.
Let me know your thoughts below…
Thanks
Luca
Contains always gets me … thanks for your thoughts and explanation on this.