How to Check If an Item Does Not Exist in an Array in PowerShell?

Do you need to check if an item does not exist in a PowerShell array? In this PowerShell tutorial, I will explain how to check if an item does not exist in an array in PowerShell using various methods with examples.

To check if an item does not exist in an array in PowerShell, use the -notcontains operator. For example, $doesNotExist = $array -notcontains ‘item’ will return $true if ‘item’ is not present in the array $array. This operator performs a case-sensitive search; for a case-insensitive check, use -inotcontains instead.

Check If an Item Does Not Exist in an Array in PowerShell

Let us check out various methods on how to check if an item does not exist in an array in PowerShell.

Using the -contains Operator

The -contains operator in PowerShell is used to check if an array contains a specific value. To check if an item does not exist in an array, you can use the -contains operator in combination with the logical not operator, !.

Here is an example:

$array = 'apple', 'banana', 'cherry'
$item = 'mango'

if (-not ($array -contains $item)) {
    Write-Host "The item '$item' does not exist in the array."
}

In this example, the script checks if the $item (‘mango’) is not in the $array. Since ‘mango’ is not an element of the $array, the condition evaluates to $true, and the message is displayed.

You can check out the screenshot below for the output after I executed the PowerShell script using Visual Studio Code.

Check If an Item Does Not Exist in an Array in PowerShell

Using the -notcontains Operator

Alternatively, PowerShell offers the -notcontains operator, which is the direct negation of -contains. It returns $true if the array does not contain the specified item.

Here, is the complete example:

$array = 'apple', 'banana', 'cherry'
$item = 'mango'

if ($array -notcontains $item) {
    Write-Host "The item '$item' does not exist in the array."
}

This does the same as the first example but uses -notcontains for a more straightforward and readable approach.

Using the .Contains() Method

The .Contains() method is a method from the [System.Collections.ArrayList] or [System.Collections.Generic.List[T]] types in .NET, which PowerShell can use to check if an array (or more accurately, a list) contains a particular value.

To use this method, you should first cast your array to a [System.Collections.ArrayList] or create a [System.Collections.Generic.List[T]] object.

Here is a complete example:

[array]$array = @('apple', 'banana', 'cherry')
$item = 'mango'

if (-not $array.Contains($item)) {
    Write-Host "The item '$item' does not exist in the array."
}

Remember that this method is case-sensitive, and the array must be cast to [array] or a list type for the .Contains() method to be available.

Using Custom Functions

If you find yourself repeatedly checking for non-existent items in arrays, you might want to create a custom function to simplify the process.

Here is a complete PowerShell custom function.

function Test-NotInArray {
    param(
        [Parameter(Mandatory=$true)]
        [array]$Array,
        [Parameter(Mandatory=$true)]
        $Item
    )
    return -not ($Array -contains $Item)
}

$array = 'apple', 'banana', 'cherry'
$item = 'mango'

if (Test-NotInArray -Array $array -Item $item) {
    Write-Host "The item '$item' does not exist in the array."
}

This custom function, Test-NotInArray, can be reused throughout your scripts for a clean and consistent approach to checking for non-existent items in PowerShell arrays.

Conclusion

Checking if an item does not exist in an array is possible using various methods. Whether you choose to use the PowerShell -contains operator with not, the -notcontains operator, the .Contains() method, or a custom function, you now have several methods to perform this check effectively.

In this PowerShell tutorial, I have explained how to check if an item does not exist in an array in PowerShell using different methods.

You may also like: