Powershell

How to use PowerShell Contains

PowerShell supports various standalone methods and operators that refine the output according to the user’s need. The operators such as Like, Match, and Contains filter the content based on matching the string expressions or objects from a collection. The functionality of these operators looks alike but they differ in targeting and extracting the content. PowerShell’s Contains operator looks for the existence of an object in a array and returns a True or False result. This article demonstrates the functionality of the Contains operator in PowerShell.

How Contains operator works in PowerShell

The syntax to use the Contains operator is provided below:

<array-object> -Contains "<expression/value>"

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.

> "PowerShell is task automation tool" -Contains "task"

A picture containing logo Description automatically generated

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:

> "Welcome", "to", "Linuxhint", "!" -Contains "Linuxhint"

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:

> $linuxhint=@("PowerShell", "automation", "tool")

Logo Description automatically generated with medium confidence

Now the Contains operator is practiced on the $linuxhint variable to look for the object named tool that resides in the $linuxhint.

> $linuxhint -Contains 'tool'

A picture containing graphical user interface Description automatically generated

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.

> "Microsoft", "PowerShell" -Contains "MiCrOsOfT"

Graphical user interface Description automatically generated with low confidence

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.

> "Microsoft", "PowerShell" -CContains "MiCrOsOfT"

A picture containing graphical user interface Description automatically generated

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.

> "Microsoft", "PowerShell" -CContains "PowerShell"

Graphical user interface Description automatically generated

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.

About the author

Adnan Shabbir