PowerShell If Variable Contains [With Examples]

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:

  • True if the item is found in the collection.
  • False if 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:

PowerShell If Variable Contains

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:

PowerShell If Variable Contains examples

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:

If Variable Contains in PowerShell

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:

powershell if variable contains substring

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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