How to use PowerShell String Contains — LazyAdmin (2024)

The Contains operator in PowerShell is a bit of a strange one. You probably want to check if a string contains a particular word or character. The -contains operator sounds the most logical one to use, but is it?

In PowerShell, the contains operator, doesn’t do substring comparison. But instead, it’s a collection operator. This means that it will test if a collection contains the matching value. It can’t be used to find a character or substring inside a string. A better solution for this is to use the -like operator or the .contains() function

In this article, I will explain how you can check if a string contains a character and explain the differences between the different operators.

PowerShell String Contains

When you want to test if a string contains a word you probably tried to use the -contains operator like this:

$string = "how to find a word"if ($string -contains "find") { # Do something}# ResultFalse

But this won’t work as you may have noticed. This is because -contains will check if the $string object matches the test element. If you would test it on the complete string, contains will return true:

$string -contains "how to find a word" # True

So when you want to search for a substring or word inside a string with PowerShell it’s better to use the -like operator. With -like you will need to use wildcards around your search string:

if ($string -like "*find*") { Write-host "String contains find"}# ResultString contains find

There is however a way to use contains in PowerShell to find a word or character inside a string. Instead of the operator, we can use the built-in .NET function .Contains() for strings. This string function can actually check if the string contains a word:

if ($string.Contains("find")) { Write-host "String contains find"}# ResultString contains find

Keep in mind that the Contains function is case sensitive, so to do a case insensitive comparison you will first need to convert the string to lower case:

if ($string.ToLower().Contains("find")) { Write-host "String contains find"}

Using -Contains and -Notcontains on Objects and Arrays

The contains operator is meant to test if an object or array contains the given value. So for example, if we have an array with fruits, we can check if the array contains a pear:

# Array with fruits$fruits = "apple","banana","pear"# Check if the array contains pear$fruits -contains "pear"# ReturnTrue

We can only search on the whole values, this means that we can’t use wildcards as with the Like operator. If we try to search on only the first few letters of apple you will see that it returns false:

$fruits -contains "*app*"# ResultFalse

If you want to check if a set doesn’t contain an element you can use the -notcontains operator. So for example, if you want to make sure that the fruits array doesn’t contain raspberry we can do the following:

$fruits -notcontains "raspberry"# ReturnsTrue

-in and -notin operators

The -in and -notin operators in PowerShell are basically the same as the -contains and -notcontains operators. The difference is that they just work the other way around. With -contains the test object is on the right-hand side of the operator. With -in, it’s on the left-hand side:

$fruits = "apple","banana","pear""apple" -in $fruits# True"raspberry -notin $fruits# True# Similar to$fruits -contains "apple"# True$fruits -notcontains "raspberry # True

Using the in operator instead of contains can improve the readability of your code. But besides that, there is no advantage in using it compared to contain.

Wrapping Up

If you want to know in PowerShell if a string contains a particular string or word then you will need to use the -like operator or the .contains() function. The contains operator can only be used on objects or arrays just like its syntactic counterpart -in and -notin.

I hope this article helped with understanding the differences between the different operators and functions. Learn more about PowerShell scripting in this complete guide. If you have any questions or tips, just drop a comment below.

0 Shares

How to use PowerShell String Contains — LazyAdmin (2024)

FAQs

How to check if a string contains a value in PowerShell? ›

The Contains method in PowerShell can be used to check if a specified string or character exists within a string, and the PowerShell operator Contains allows you to check if a collection of objects, such as an array, contains a specific value.

How do I extract part of a string in PowerShell? ›

To extract a substring after a specific character in PowerShell by using the Substring() method combined with the IndexOf() method. Here's an example: $FullString = "Hello,World!" This will start extracting from character 5 and extract all the characters from the given string.

How to check string content in PowerShell? ›

If you want to know in PowerShell if a string contains a particular string or word then you will need to use the -like operator or the . contains() function. The contains operator can only be used on objects or arrays just like its syntactic counterpart -in and -notin .

How do you check if a collection contains a value in PowerShell? ›

The simplest way to check if an array contains a certain value in PowerShell is by using the -contains operator. This operator returns $true if the array contains the specified value, and $false otherwise.

How do you check if a string contains something? ›

You can use JavaScript's includes() method to check whether a string contains a substring. This will return true if the substring is found, or false if not. const str = 'This is my example string!'; const substr = 'my'; console.log(str.includes(substr)); The above code would output true .

How do you check string contains or not? ›

Java String contains() Method

The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

How do you extract a portion of a string? ›

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

How do you get a part of a string in string? ›

String.prototype.substring() The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.

How to use contains in PowerShell? ›

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. Now, run the following command in PowerShell to test whether Java exists in the collection.

How to use string in PowerShell? ›

A String can be defined in PowerShell by using the single or double-quotes. Both the strings are created of the same System. String object type.

What is the difference between contains and CContains? ›

The -Contains operator compares one object against a collection. It returns true if that entire value exists as an item in a collection. It is case-insensitive and is not limited to strings. -CContains is the case-sensitive version.

How to check if a string is in an array in PowerShell? ›

You can use the GetType() method on just about anything to see what kind of variable it is (string or array). Probably the easiest way to see if an array has more then one element in it is to use the COUNT property ($array. Count).

How to check value in PowerShell? ›

The Get-Variable cmdlet gets the Windows PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name.

How to check if a value exists in an array in PowerShell? ›

The -contains operator allows you to check an array of values to see if it contains a specified value.

How to check not contain in PowerShell? ›

Using the PowerShell Not Contains Operator ( -notcontains )

For instance, suppose you create an array of numbers from 1 to 10, as shown below. The expected result is True since the number 4 exists in the array. To check if a value does not exist in an array, run the -notcontains operator instead.

Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6294

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.