How to Check if an Array Does Not Contain a String in PowerShell?

In this PowerShell tutorial, I will explain how to check if an array does not contain a string in PowerShell.

To verify that an array does not contain a specific string in PowerShell, you can use the -notcontains operator for a case-sensitive check. For example, $notPresent = $array -notcontains ‘specificString’ will return $true if ‘specificString’ is not in the array $array. For case-insensitive checks, replace -notcontains with -inotcontains.

Check if an Array Does Not Contain a String in PowerShell

Now, let us discuss various methods of how to check if an array does not contain a string in PowerShell.

Using the -notcontains Operator

PowerShell provides a straightforward operator for checking if an array does not contain a specific element: the -notcontains operator. This operator returns True if the array does not include the specified value, and False otherwise.

Here is a simple example:

# Define an array of strings
$array = @('apple', 'banana', 'cherry')

# Check if the array does not contain the string 'banana'
$result = $array -notcontains 'banana'

# Output the result
$result  # This will output False because 'banana' is in the array

In this example, we defined an array $array that contains three fruit names. We then used the -notcontains operator to check if ‘banana’ is not in the array. Since ‘banana’ is indeed in the array, $result will be False.

Now, let’s check for a string that is not in the array:

# Check if the array does not contain the string 'date'
$result = $array -notcontains 'date'

# Output the result
$result  # This will output True because 'date' is not in the array

In this case, ‘date’ is not an element of $array, so $result will be True.

I executed the PowerShell script using VS code, and you can see the output in the screenshot below:

Check if an Array Does Not Contain a String in PowerShell

Using Custom Filtering with Where-Object

Another way to determine if an array does not contain a string is to use the Where-Object cmdlet in PowerShell. This cmdlet allows you to filter arrays based on custom conditions.

Here’s how you can use Where-Object for our purpose:

# Define an array of strings
$array = @('apple', 'banana', 'cherry')

# Use Where-Object to filter the array
$result = $array | Where-Object { $_ -ne 'banana' }

# Check if the result contains any elements
if ($result.Count -gt 0) {
    $containsOtherThanBanana = $true
} else {
    $containsOtherThanBanana = $false
}

# Output the result
$containsOtherThanBanana # This will output True

In this snippet, we piped $array into Where-Object, specifying a script block { $_ -ne 'banana' } that filters out ‘banana’. The $_ variable represents the current item in the pipeline. If $result has more than zero elements, it means the array contains elements other than ‘banana’.

Using Loops for Custom Logic

Sometimes, you might want to perform more complex checks or actions if a PowerShell array does not contain a particular string. In such cases, a loop can be helpful.

Consider the following example using a foreach loop:

# Define an array of strings
$array = @('apple', 'banana', 'cherry')

# Variable to hold the check result
$notContainsString = $true

# Loop through each item in the array
foreach ($item in $array) {
    if ($item -eq 'banana') {
        # If 'banana' is found, set the flag to False
        $notContainsString = $false
        break  # Exit the loop
    }
}

# Output the result
$notContainsString # This will output False

In this script, we loop through each element of $array and check if it equals ‘banana’. If we find ‘banana’, we set $notContainsString to False and exit the loop early with break.

Conclusion

Checking if an array does not contain a specific string is a common task in PowerShell scripting. The -notcontains operator is the most direct way to perform this check. For more complex scenarios or when you need to perform additional actions based on the check, you can use Where-Object or loops along with conditional logic.

In this PowerShell script, I have explained how to check if an array does not contain a specific string in PowerShell.

You may also like: