---
title: "Back to Basics: The PowerShell Contains Operator"
description: "Keep your PowerShell fundamentals strong with this tutorial on the PowerShell contains operator by ATA Learning!"
canonical: "https://adamtheautomator.com/powershell-contains-operator/"
---

# Back to Basics: The PowerShell Contains Operator

> Keep your PowerShell fundamentals strong with this tutorial on the PowerShell contains operator by ATA Learning!

Source: https://adamtheautomator.com/powershell-contains-operator/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Back to Basics: The PowerShell Contains Operator](https://adamtheautomator.com/wp-content/uploads/2022/11/Back-to-Basics-The-PowerShell-Contains-Operator.jpg)

# Back to Basics: The PowerShell Contains Operator

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)10 November 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)[PowerShell Language](/tag/powershell-language/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Using the PowerShell Contains Operator (-contains)](#using-the-powershell-contains-operator--contains)
*   [Testing a Collection of Values as Input](#testing-a-collection-of-values-as-input)
*   [Testing an Array Variable as Input](#testing-an-array-variable-as-input)
*   [Performing a Case-Sensitive Comparison](#performing-a-case-sensitive-comparison)
*   [Using the PowerShell Not Contains Operator (-notcontains)](#using-the-powershell-not-contains-operator--notcontains)
*   [Comparing the PowerShell Contains Operator with the Like Operator](#comparing-the-powershell-contains-operator-with-the-like-operator)
*   [Conclusion](#conclusion)

As more information becomes available, finding what you are looking for is increasingly difficult. When all you need is to determine whether a value exists in a collection, in that case, the _PowerShell Contains Operator_ `(-contains)` can help accomplish this task.

In this post, you will learn how to use the PowerShell contains operator when determining the existence of value. You’ll also discover its inverse, the `-notcontains` operator.

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have Windows PowerShell 5.1 or PowerShell 7+. This tutorial uses v7.2.6.

Related:[PowerShell 7 Upgrade : A How to Walk Through](https://adamtheautomator.com/powershell-7-upgrade/)

## Using the PowerShell Contains Operator (`-contains`)

The PowerShell Contains operator is one of the [Containment Operators](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.2#containment-operators). This operator determines whether a value exists in a given set. The result does not show the matching value. Instead, it returns a Boolean value (`True/False`), indicating whether or not the test value exists.

The syntax for `-contains` is as follows:

```powershell
<Collection> -Contains <Test-Value>
```

*   `<Collection>` represents the set of objects for comparison. The set comes before the `-contains` operator and can be the literal values or a variable containing the set.
*   `<Test-Value>` is the value to check if it exists in the set. The value should be after the `-contains` operator (to the right) and can be a literal value, a collection of values, or a variable.

How can you apply the PowerShell contains operator? You’ll learn about them in the next sections.

### Testing a Collection of Values as Input

You can directly compare a test object against a collection of values, like a string collection, in this case, using the `-contains` operator. This method is helpful when you have a list of items and want to know if a specific item is on the list.

The below example shows the PowerShell Contains operator in action to check if a value exists in a collection:

```powershell
"PowerShell", "CSS", "HTML", "NodeJS" -contains "PowerShell"
```

Note that each value in the collection is comma-separated and encloses each element in quotation marks. The `-Contains` operator checks whether the test value exists or is equal to an element of the collection.

In this case, the command returns `True` because `PowerShell` exists in the collection, as shown below.

![Comparing Strings using the PowerShell Contains Operator](https://adamtheautomator.com/wp-content/uploads/2022/11/image-81.png)

Comparing Strings using the PowerShell Contains Operator

Now, run the following command in PowerShell to test whether `Java` exists in the collection.

```powershell
"PowerShell", "CSS", "HTML", "NodeJS" -contains "Java"
```

The expected result is `False` because `Java` does not exist in the collection as shown.

![The test value does not exist in the collection of strings](https://adamtheautomator.com/wp-content/uploads/2022/11/image-82.png)

The test value does not exist in the collection of strings

Apart from strings, you can also use integers as the collection and test value. For example, the below command tests whether `3` exists in the collection of integers from `1` through `4`.

```powershell
1,2,3,4 -contains 3
```

As expected, since `3` exists in the collection, the result should be `True`.

![Testing with integers](https://adamtheautomator.com/wp-content/uploads/2022/11/image-83.png)

Testing with integers

### Testing an Array Variable as Input

Variables are common, useful, and often unavoidable, especially in scripts. As such, the PowerShell contains operator can also test against the elements in a variable.

Related:[Build Better Scripts with PowerShell ArrayLists and Arrays](https://adamtheautomator.com/powershell-array/)

For example, run the below command to create an array containing a list of programming languages and store it in the `$languages` variable.

```powershell
$languages = "PowerShell", "CSS", "HTML", "NodeJS"
```

Now, test whether `PowerShell` exists as an element in the `$languages` variable.

```powershell
$languages -contains "PowerShell"
```

The result is `True` because `PowerShell` is one of the values in the array, as shown below.

![Testing an array variable](https://adamtheautomator.com/wp-content/uploads/2022/11/image-84.png)

Testing an array variable

Run the commands below to check for other values in the array, such as `Java`, `CSS`, and `HTML`.

```powershell
$languages -contains "Java"
$languages -contains "CSS"
$languages -contains "HTML"
```

![Using PowerShell Contains Operator on an array](https://adamtheautomator.com/wp-content/uploads/2022/11/image-85.png)

Using PowerShell Contains Operator on an array

### Performing a Case-Sensitive Comparison

By default, the `-contains` operator is case-insensitive, which means the operator treats uppercase and lowercase characters the same. For instance, the code snippet below creates an array consisting of the names of fruits in all lowercase:

```powershell
$fruits = "apple", "banana", "cherry"
```

What do you think is the result of checking `APPLe` against the collection by running the code below?

```powershell
$fruits -contains "APPLe"
```

The result? The test value `APPle` equals `apple`, and `apple` exists in the collection, so the result is `True`.

![Performing In-sensitive Search](https://adamtheautomator.com/wp-content/uploads/2022/11/image-86.png)

Performing In-sensitive Search

What if you don’t want PowerShell to ignore the character case differences? In this instance, replace `-contains` with `-ccontains`. Unlike `-contains`, the `-ccontains` operator performs case-sensitive containment comparison.

Like the command below, replace `-contains` with `-ccontains` and run it in PowerShell.

```powershell
$fruits -ccontains "Apple"
```

This time, the result is `False` because of the case-sensitive comparison.

![Performing a Case-Sensitive Search](https://adamtheautomator.com/wp-content/uploads/2022/11/image-87.png)

Performing a Case-Sensitive Search

## Using the PowerShell Not Contains Operator (`-notcontains`)

Simply put `-notcontains` is the inverse of `-contains`. As such, the `-notcontains` operator returns `True` when the value is not in the collection and `$false` when the value exists in the collection.

For instance, suppose you create an array of numbers from 1 to 10, as shown below.

```powershell
$numbers = 1..10
```

To check if `4` exists in the array, run the `-contains` operator like so:

```powershell
$numbers -contains 4
```

The expected result is `True` since the number 4 exists in the array.

![Using the PowerShell contains operator](https://adamtheautomator.com/wp-content/uploads/2022/11/image-88.png)

Using the PowerShell contains operator

To check if a value does not exist in an array, run the `-notcontains` operator instead.

```powershell
$numbers -notcontains 4
```

This time, the result is `False` because `4` exists in the array.

![Using the -notcontains Operator](https://adamtheautomator.com/wp-content/uploads/2022/11/image-89.png)

Using the -notcontains Operator

## Comparing the PowerShell Contains Operator with the Like Operator

The `-contains` operator and `-like` operator are both PowerShell comparison operators. But `-like` is not a containment operator and instead a [matching operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.2#matching-operators).

The `-like` operator matches the wildcard expression against a string or string array. In contrast, the `-contains` operator does not work wildcard characters as `-like` does.

If the input is an array, `-like` returns the array member that matches the pattern. But if the input is scalar or a single string object, the result is a Boolean value instead. The below snippet shows the `-like` operator syntax.

```bash
<string[]> -like <wildcard-expression> # output is the matching item.
<string> -like <wildcard-expression> # output is Boolean (True/False).
```

For instance, run the command below to match the pattern `*string` with the single string input.

```powershell
"This is a sample string" -like "*string"
```

As you can see, the result is a Boolean (`True`) because the input is a single-string object and matches the wildcard pattern.

![Using the -like operator](https://adamtheautomator.com/wp-content/uploads/2022/11/image-90.png)

Using the -like operator

If the input is a string array, like the command below.

```bash
$inputarray = "This is a sample string", "This is an integer"
$inputarray -like "*string"
```

The result would return the array member that matches the pattern instead.

![The -like operator uses an array as input](https://adamtheautomator.com/wp-content/uploads/2022/11/image-91.png)

The -like operator uses an array as input

For comparison, replace `-like` with `-contains`, and the logic fails because `-contains` does not support wildcard expressions.

```powershell
"This is a sample string" -contains "*string"
```

![PowerShell Contains operation does not support wildcard expressions](https://adamtheautomator.com/wp-content/uploads/2022/11/image-92.png)

PowerShell Contains operation does not support wildcard expressions

In conclusion, use `-contains` to check if a value exists in an array and `-like` operator to check if a string matches a wildcard pattern expression.

## Conclusion

The PowerShell contains operator helps check a value’s existence in a collection when all you need is a `True` or `False` result. The `-contains` operator may seem to do so little on its own. But it plays a critical part when it comes to scripting and automation.

But don’t stop with the PowerShell contains operator. With the proper usage, other [comparison operators](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators) can complement what you’ve learned in this article to help you build fantastic PowerShell scripts!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-contains-operator%2F&text=Back%20to%20Basics%3A%20The%20PowerShell%20Contains%20Operator)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-contains-operator%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-contains-operator%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2023/08/powershell-approved-verbs.jpg)

### [Your Getting Started Guide to PowerShell Approved Verbs](/powershell-approved-verbs/)

Discover how to get started with PowerShell Approved Verbs to make sure your scripts and code is top-notch in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2023/07/powershell-sort-object.jpg)

### [Learning PowerShell Sort-Object with Examples](/powershell-sort-object/)

Learn all of the ins-and-outs of the PowerShell Sort-Object cmdlet in this example driven tutorial by ATA Learning!

![](https://adamtheautomator.com/wp-content/uploads/2023/03/powershell-change-directory.jpg)

### [PowerShell Change Directory: Navigating Your File System](/powershell-change-directory/)

Learn how to use the PowerShell change directory command to navigate your file system with ease. Master the basics of PowerShell file navigation today.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
