PowerShell Where-Object Contains Examples

PowerShell Where-Object cmdlet allows you to filter objects based on their properties. In this PowerShell tutorial, we will focus on the Contains operator within the Where-Object cmdlet and how you can use it. I will also show you PowerShell Where-Object contains examples.

In PowerShell, the Where-Object cmdlet is often used to filter objects based on their properties. To check if a property that is a collection (such as an array) contains a specific value, you use the -contains operator. For example, to find all processes that have a certain module loaded, you could use Get-Process | Where-Object { $_.Modules -contains ‘ModuleName’ }.

PowerShell Where-Object Contains

The Contains operator is used with PowerShell Where-Object to find objects in a collection that include a particular property value. It’s important to note that Contains is different from the -like operator, which supports wildcard comparisons, or -match, which uses regular expressions.

A common use case for Contains is when you’re dealing with a collection or an array of values within a single property of an object. Let’s say you want to find all processes that have a certain module loaded. You could do something like this:

Get-Process | Where-Object { $_.Modules -Contains 'ModuleName' }

In this example, Modules is a property that contains a collection of all the modules that each process has loaded. The Contains operator checks to see if ‘ModuleName’ is in that collection.

Here is an example:

Imagine you have a directory of files, and you want to find all files that contain a specific string in their name. You can use a filter with contains in Where-Object.

Get-ChildItem | Where-Object { $_.Name -Contains 'Report' }

This command retrieves all items in the current directory and filters them based on whether their name contains the string ‘Report’.

When using Contains in Where-Object in PowerShell, remember that it’s case-sensitive and it looks for an exact match within a collection. If you need a case-insensitive search or partial matching, you might want to consider using -like with wildcards or -match with a regular expression instead.

It’s also worth noting that Contains is not the same as In, which checks if a single value is in a collection. The syntax and use cases are different:

'ServiceName' -In (Get-Service).Name

This checks if ‘ServiceName’ is in the collection of names of all services.

PowerShell where-object contains string

When working with PowerShell, you might need to filter objects based on whether a string property contains a particular substring. This is where the Where-Object cmdlet comes into play, allowing you to sift through a collection of objects and pick out the ones that match certain criteria.

However, there’s a common misconception when it comes to using the term “contains” with Where-Object. In PowerShell, the -contains operator is used to check for the presence of a value within a collection (array), not for substring checking within a string property. To check if a string property of an object contains a particular substring, you would typically use the -like operator with wildcard characters, or the -match operator which uses regular expressions.

Here’s an example of using Where-Object with the -like operator to find a string within another string:

Get-Process | Where-Object { $_.ProcessName -like '*notepad*' }

In this example, we’re filtering the output of Get-Process to only show processes where the ProcessName property contains the substring “notepad”. The asterisks (*) are wildcard characters that match zero or more characters on either side of “notepad”.

You can see the output in the screenshot below:

PowerShell Where-Object Contains

If you need to perform a case-insensitive substring search, -like and -match are inherently case-insensitive in PowerShell. However, you can use the -ccontains-clike, or -cmatch operators for case-sensitive comparisons.

For example, using -match to find a string within another string:

Get-Service | Where-Object { $_.DisplayName -match 'remote' }

This would filter services to those that have “remote” in their DisplayName.

It’s important to note that -match uses regular expressions, which are powerful pattern-matching tools that go beyond simple substring searches. This can be very useful but also requires some understanding of regular expression syntax to use effectively.

In summary, when you want to filter objects in PowerShell based on whether a string property contains a specific substring, you should use -like or -match, not -contains. The -contains operator is intended for use with collections, not strings.

PowerShell where-object not contains

In PowerShell, when you want to filter objects where a property does not contain a specific value, you might think of using a “not contains” operator. However, PowerShell does not have a -notcontains operator for strings. Instead, you need to use the -notlike operator or -notmatch for negating a string match, both of which can be used in conjunction with the Where-Object cmdlet.

Using -notlike

The -notlike operator is used with Where-Object to exclude objects where a string property does not match a specified pattern, which can include wildcards. Here’s an example:

Get-Service | Where-Object { $_.DisplayName -notlike '*SQL*' }

This command will return all services where the DisplayName does not include the substring “SQL”.

Using -notmatch

Alternatively, -notmatch uses regular expressions for pattern matching and can be used to perform more complex negations:

Get-Process | Where-Object { $_.ProcessName -notmatch '^conhost$' }

The above command will return a list of processes where the ProcessName does not exactly match “conhost”. The ^ and $ are regular expression anchors for the start and end of the string, respectively.

Dealing with Collections

If you’re dealing with a property that is a collection (array) and you want to exclude objects where the collection contains a certain value, you use -notcontains. This is the correct use of -notcontains in PowerShell:

$myArray = 'apple', 'banana', 'cherry'
$myArray -notcontains 'orange' # This returns True because 'orange' is not in the array

However, when filtering with Where-Object, you would do something like this:

$myObjects | Where-Object { $_.SomeCollectionProperty -notcontains 'SomeValue' }

In this example, $myObjects is a collection of objects, each with a property SomeCollectionProperty that is itself a collection. The -notcontains operator is used to filter out the objects where SomeCollectionProperty does not contain ‘SomeValue’.

When you want to filter objects based on the absence of a substring within a string property, you should use -notlike or -notmatch with Where-Object in PowerShell. When dealing with a collection property and you want to exclude objects where the collection contains a specific value, you would use -notcontains.

PowerShell where-object does not contain

In PowerShell, when you need to filter objects based on the absence of a certain value within a property that is a collection (like an array), you can use the -notcontains operator with the Where-Object cmdlet in PowerShell. However, if you’re looking to check if a string property does not contain a specific substring, you will need to use the -notlike or -notmatch operators instead, as -notcontains is not designed for string containment checks.

Using -notlike for Strings

The -notlike operator is used in string comparisons where you want to return objects that do not match a specific pattern. This pattern can include wildcards, such as the asterisk (*), which represents any number of characters. Here’s an example:

Get-ChildItem | Where-Object { $_.Name -notlike '*temp*' }

This command will return all items where the Name property does not include the substring “temp”.

Using -notmatch for Strings

The -notmatch operator uses regular expressions for pattern matching and is used to exclude objects where a string property matches a specified regex pattern. Here’s an example:

Get-Process | Where-Object { $_.ProcessName -notmatch 'chrome' }

This command will return all processes where the ProcessName does not contain the substring “chrome”.

Using -notcontains for Collections

When dealing with properties that are collections, the -notcontains operator is appropriate. It checks if a collection does not contain a specific value. For example:

$fruitList = 'apple', 'banana', 'cherry'
$fruitList -notcontains 'grape' # This returns True because 'grape' is not in the list

In the context of Where-Object, you can use -notcontains to filter out objects where a collection-type property does not contain a certain value:

$myObjects | Where-Object { $_.FruitList -notcontains 'grape' }

In this example, $myObjects might be a collection of objects, each with a property FruitList that is a collection of strings representing fruits. The -notcontains operator is used to return only the objects where FruitList does not contain the string “grape”.

When you want to filter out objects in PowerShell where a string property does not contain a certain substring, you should use -notlike or -notmatch. When you need to exclude objects based on the absence of a value within a collection property, use -notcontains.

PowerShell where-object contains examples

Let us check out two examples related to PowerShell where-object contains.

Example 1: Filtering an Array with -contains

Let’s say you have an array of numbers, and you want to find out if the array contains a specific number:

$numbers = 1, 2, 3, 4, 5
$numbers -contains 3

This will return True because the number 3 is in the array $numbers.

Example 2: Filtering Objects with a Property Containing a Certain Value

If you have a custom object with a property that is an array, and you want to filter those objects based on whether the array contains a certain value, you would use -contains in a Where-Object block like this:

$myObjects | Where-Object { $_.PropertyArray -contains 'Value' }

In this example, $myObjects might be a collection of objects, and PropertyArray is a property on those objects that is an array. The command filters for objects where PropertyArray contains the string “Value”.

Conclusion

The Where-Object cmdlet with the Contains operator is very useful in PowerShell for filtering objects based on the contents of their properties. Also, we discussed the below topics:

  • PowerShell where-object contains
  • PowerShell where-object contains string
  • PowerShell where-object not contains
  • PowerShell where-object does not contain
  • PowerShell where-object contains examples

You may also like: