Previously I wrote a complete tutorial on PowerShell -contains Operator. Someone in my team asked more about PowerShell if variables contains. I thought I would explain everything about it with examples.
To check if a variable contains a specific value in PowerShell, you can use the -contains operator. This operator is used to determine if an array or collection includes a particular item. For example, if you have an array $cities = @("New York", "Los Angeles", "Chicago", "Houston") and want to check if it contains “Chicago”, you would use if ($cities -contains "Chicago") { Write-Output "Chicago is in the list." }. This will return True if “Chicago” is present in the array.
PowerShell If Variable Contains
Before checking all the examples, you should know what the PowerShell -contains operator does.
In PowerShell, the -contains operator determines if a collection of objects, such as an array, includes a specific item. This operator is particularly useful when you need to check for the presence of an element within a collection.
The syntax for using the -contains operator is straightforward:
$collection -contains $item$collection: This is the array or collection of items you want to check.$item: This is the value you are searching for within the collection.
The -contains operator returns a Boolean value:
Trueif the item is found in the collection.Falseif the item is not found in the collection.
Now, let me show you a few real examples related to if variable contains in PowerShell.
Example 1: Check if an Array Contains a Specific Value
Imagine you have an array of city names in the USA, and you want to check if it contains “Seattle”. This way, you can check if a PowerShell variable contains a specific value using the below script.
$cities = @("New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Seattle")
if ($cities -contains "Seattle") {
Write-Output "Seattle is in the list."
} else {
Write-Output "Seattle is not in the list."
}In this example, the output will be Seattle is in the list. because “Seattle” is indeed an element of the $cities array.
You can see the exact output in the screenshot below:

Check out PowerShell Switch String Contains
Example 2: Using -contains with Numbers
You can also use the -contains operator with numbers. For instance, checking if a list of numbers includes the number 42:
$numbers = @(10, 20, 30, 40, 50)
if ($numbers -contains 42) {
Write-Output "42 is in the list."
} else {
Write-Output "42 is not in the list."
}In this case, the output will be 42 is not in the list. because 42 is not an element of the $numbers array.
You can check out the output in the screenshot below:

Check out Check if a String Contains a Space in PowerShell
Example 3: Case Sensitivity in Strings
The -contains operator is case-insensitive when working with strings. Let’s see an example:
$states = @("California", "Texas", "Florida", "New York")
if ($states -contains "texas") {
Write-Output "Texas is in the list."
} else {
Write-Output "Texas is not in the list."
}The output will be Texas is in the list. because the comparison is case-insensitive.
Here is the output you can see in the screenshot below:

Example 4: Check for Substrings
Let me show you another example for “PowerShell if variable contains’.
The -contains operator does not support substring comparisons. If you need to check if a string contains a substring, you should use the -like or -match operators instead. Here’s how you can do it:
$name = "John Doe"
if ($name -like "*Doe*") {
Write-Output "The name contains 'Doe'."
} else {
Write-Output "The name does not contain 'Doe'."
}Example 5: Using -contains with Custom Objects
You can also use the -contains operator with custom objects. Let’s consider an example where you have a list of user objects, and you want to check if a user with a specific username exists:
$users = @(
[PSCustomObject]@{Username="jsmith"; FullName="John Smith"},
[PSCustomObject]@{Username="adoe"; FullName="Alice Doe"},
[PSCustomObject]@{Username="bwhite"; FullName="Bob White"}
)
if ($users.Username -contains "adoe") {
Write-Output "User 'adoe' exists."
} else {
Write-Output "User 'adoe' does not exist."
}In this example, the output will be User 'adoe' exists. because “adoe” is a username in the $users collection.
Here is the output you can see in the screenshot below:

Conclusion
In this tutorial, I explained about PowerShell if the variable contains, with some real examples. I have explained how to work with different PowerShell variables, like arrays, numbers, strings, custom objects, etc. Still, have some questions? Leave a comment below, I will try to reply you ASAP.
You can check some related tutorials:
- Check if a String Contains Only Numbers in PowerShell
- Check if a String Contains Multiple Values in PowerShell
- Check if a String Contains Special Characters in PowerShell
- PowerShell If and
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.