Search notes:

PowerShell: -contains operator

Check for existence of value in array

The following example tries to demonstrate how -contains can be used to check if a given value exists in an array.
$primes = 'two', 'three', 'five', 'seven', 'eleven', 'thirteen'

function check($num) {
   if ($primes -contains $num) {
      echo "$num seems to be a small prime"
   }
   else {
      echo "$num does not seem to be a small prime"
   }
}

check 'one'
check 'two'
check 'three'
check 'four'
Github repository about-PowerShell, path: /language/operator/contains/value-in-array.ps1

-contains vs -like

The -contains operator must not be confused with the -like operator. -like compares strings while -contains checks for the existence of an object in a collection.
"hello world" -contains '*llo*'  # False
"hello world" -like     '*llo*'  # True

$items = 'foo', 'bar', 'baz'
$items -contains 'bar'           # True
$items -contains 'xyz'           # False
Github repository about-PowerShell, path: /language/operator/contains/like-vs-contains.ps1

Misc

-contains returns $True if every property of the compared object match.
-contains has an opposite: -notContains.

See also

-in

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1759383949, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/language/operator/contains(75): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78