The -in Operator For PowerShell

PowerShell provides the -in containment operator in order to check if the specified item exists in the specified collection. The in operator execution returns a boolean value True or False . If the specified item exists in the specified collection or list it returns the value True. If the specified item does not exist in the specified collection or list it returns the value False .

-in Operator Syntax

The PowerShell in operator has the following syntax where the search item and collection is provided.

ITEM -in COLLECTION
  • ITEM is searched in the COLLECTION where it may contains single or more items. The item cna be a string, an object, a number etc.
  • COLLECTION may contain single or more items. The collection may contain single or more items in different formats like string, object, number.

Check Item If Exist In Specified Collection

The in operator is used to check if the specified item exists in the specified collection. In the following example we check if the “ismail” existing the collection “ahmet”,”ali”,”elif”,”ismail”.

$result = "ismail" -in "ahmet","ali","elif","ismail"

Write-Output $result

The output is like below.

True

In the following example we check if the specified number exist in the given number collection.

$result = 5 -in 1,5,7,10

Write-Output $result

The output is True as the 5 exist in the collection.

Check Items If NOT Exist In Specified Collection

There is the -notin operator where it is the reverse of the -in operator. The -notin operator is used to check if the specified item not exist in the given collection. If the specified item is not exist in the specified collection this expression returns the True .

$result = "ismail" -notin "ahmet","ali","elif","ismail"

Write-Output $result

The output is False as the “ismail” exist in the collection.

See also  Windows ipconfig Command Tutorial

In the following example we check if the specified number do not exist in the given collection.

$result = 5 -in 1,3,7,10

Write-Output $result

The output is True as the 5 do not exist in the specified collection.

Check Multiple Items If Exist In Specified Collection

Another useful feature of the -in operator is it can be used to check multiple items in a collection. If all of the specified items exist in the given collection the result is True .

$result = "ismail","ali" -in "ahmet","ali","elif","ismail"

Write-Output $result

The output is True .

$result = "ismail","mehmet" -in "ahmet","ali","elif","ismail"

Write-Output $result

The output is True . Even the “ismail” exist in the given collection the “mehmet” does not exist.

Check Multiple Items If NOT Exist In Specified Collection

Also the -notin operator can be used to check if multiple items do not exist in the specified collection. If one of the items of the specified items do not exist the -notin operator returns True .

$result = "ismail","mehmet" -notin "ahmet","ali","elif","ismail"

Write-Output $result

The output is True as the “mehmet” do not exist in the specified collection.

Leave a Comment