PowerShell -contains Operator [With Examples]

As a PowerShell developer, you should know how to use the -contains operator in PowerShell. In this tutorial, I will explain how to use the PowerShell -contains operator with examples.

What is the -contains Operator in PowerShell?

The -contains operator in PowerShell is used to determine whether a specified item exists within a collection, such as an array or a list. It returns a Boolean value: $true if the item is found, and $false if it is not.

This operator is particularly useful when you need to check for the presence of an element in a collection without iterating through each item manually.

Syntax of the -contains Operator

The syntax for the -contains operator is:

<collection> -contains <item>
  • <collection>: This is the array or collection you are searching through.
  • <item>: This is the item you are checking for within the collection.

Read PowerShell Like Operator

PowerShell -contains Operator Examples

Now, let us understand more by checking a few examples of the -contains operator in PowerShell.

We will start with a basic example

Example-1: Basic Example

Here is a basic example of how to use the -contains operator in PowerShell.

# Define an array of numbers
$numbers = 1, 2, 3, 4, 5

# Check if the number 3 is in the array
if ($numbers -contains 3) {
    Write-Output "The array contains the number 3."
} else {
    Write-Output "The array does not contain the number 3."
}

In this example, the script checks if the number 3 is present in the $numbers array. Since 3 is indeed in the array, the output will be:

The array contains the number 3.

I executed the above script, and you can see the exact output in the screenshot below:

PowerShell -contains Operator

Check out Check if a String Contains a Space in PowerShell

Example-2: Using -contains with Strings

Let me show you another example. Here, I will show you how to use the -contains operator with strings in PowerShell.

The -contains operator can also be used with arrays of strings. However, it’s important to note that -contains does not perform substring matching; it looks for exact matches only.

# Define an array of strings
$fruits = "apple", "banana", "cherry"

# Check if the array contains the string "banana"
if ($fruits -contains "banana") {
    Write-Output "The array contains 'banana'."
} else {
    Write-Output "The array does not contain 'banana'."
}

This script will output:

The array contains 'banana'.

You can see the output in the screenshot below after I executed the above script using vs code.

contains Operator in PowerShell

Check out -and Operator in PowerShell

Example-3: Case Sensitivity

By default, the -contains operator is case-insensitive. However, if you need to perform a case-sensitive comparison, you can use the -ccontains operator like the below example.

# Define an array of strings
$fruits = "Apple", "Banana", "Cherry"

# Check if the array contains the string "banana" (case-sensitive)
if ($fruits -ccontains "banana") {
    Write-Output "The array contains 'banana'."
} else {
    Write-Output "The array does not contain 'banana'."
}

Since -ccontains is case-sensitive, and there is no exact match for “banana” (with a lowercase ‘b’), the output will be:

The array does not contain 'banana'.

You can see the exact output in the screenshot below.

PowerShell -contains Operator Examples

Example-4: Check for Objects in Arrays using the -contains operator

Now, let me show you one advanced example of using the -contains operator in PowerShell.

The -contains operator can also be used to check for objects within an array of objects.

Here is a complete example.

# Define an array of custom objects
$people = @(
    [PSCustomObject]@{ Name = "Alice"; Age = 30 },
    [PSCustomObject]@{ Name = "Bob"; Age = 25 },
    [PSCustomObject]@{ Name = "Charlie"; Age = 35 }
)

# Define a person object to search for
$searchPerson = [PSCustomObject]@{ Name = "Bob"; Age = 25 }

# Check if the array contains the specified person object
if ($people -contains $searchPerson) {
    Write-Output "The array contains the specified person."
} else {
    Write-Output "The array does not contain the specified person."
}

In this example, the script creates an array of custom objects representing people and then checks if a specific person object is present in the array. The output will be:

The array contains the specified person.

Conclusion

The -contains operator in PowerShell checks the presence of an item within a collection. In this tutorial, I have explained how to use the PowerShell -contains operator with four different examples.

Still, have some questions? Feel free to drop a comment below.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.