How Contains operator works in PowerShell
The syntax to use the Contains operator is provided below:
The array object can be passed directly, or you can store the objects as an array variable. After that, the Contains operator is used on it. Moreover, the expression/value in the above-mentioned syntax denotes the value that you want to search in the collection of objects.
The PowerShell contains operator returns either True or False, depending upon the satisfaction of the condition. If the expression matches the object from the collection, then the Contains operator prints True and if the value does not match then you would get a False keyword in the output.
How to use PowerShell Contains
This section guides you to perform some practicality on the Contains operator. For this, we have provided a list of examples and each example shows the usage of the Contains operator in a different perspective.
Contains operator does not work on Strings
Before getting into the examples, let’s practice the Contains operator on a string and check the output. The output returned by the command is False which means the Contains operator is not executed.
Example 1: Passing a collection of objects with the PowerShell Contains operator
For instance, we have created a collection of objects that comprises of three values: “Welcome“, “to“, “Linuxhint“, “!“. This collection of objects is examined using the Contains operator. In the below-stated command, the Contains operator looks for the “Linuxhint” object. The output is True which means the Contains operator has been executed successfully:
Example 2: Passing an array variable with PowerShell Contains operator
The PowerShell command-line tool allows you to create variables as well. We have created an array variable in this example that contains string values as objects. The following command assisted us to create an array object:
Now the Contains operator is practiced on the $linuxhint variable to look for the object named tool that resides in the $linuxhint.
Example 3: Case sensitive of PowerShell Contains operator
If the Contains operator is used generally as we did in the previous two examples, then it acts as a case-insensitive operator. For instance, the following command executes the Contains operator on a collection of objects without throwing any case sensitivity error.
For making Contains a case-sensitive operator, we would add an extra “C” to the operator, and it would become “CContains“. Now, execute the same command (above) with the “CContains” operator. The output has returned False which means the Contains operator is not working.
To execute the command successfully with “CContains“, you must take care of the case sensitivity. The “CContains” operator will only be executed if the values have the same case as in the collection of objects. The below-stated command would return True because we have used the same case as in the object’s collection.
Conclusion
PowerShell Contains is a handy utility to filter the content from a collection of objects. In this post, you have learned the working of PowerShell Contains operator according to the syntax. For better understanding, we have demonstrated the functionality of the Contains operator in multiple scenarios. The Contains operator functions perfectly when applied to a collection of objects and therefore is meant for objects only. It is observed that the built-in support of the Contains operator is case insensitive. However, the Contains operator can be made sensitive by whereas the CContains perform the same action by keeping the case sensitivity in practice.