Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to determine whether an array or list contains a specific item.
To determine whether a list contains a specific item, use the -contains
operator:
PS > "Hello","World" -contains "Hello" True PS > "Hello","World" -contains "There" False
Alternatively, use the -in
operator, which acts like the -contains
operator with its operands reversed:
PS > "Hello" -in "Hello","World" True PS > "There" -in "Hello","World" False
The -contains
and -in
operators are useful ways to quickly determine whether a list contains a specific element. To search a list for items that instead match a pattern, use the -match
or -like
operators.
For more information about the -contains
, -in
, -match
, and -like
operators, see “Comparison Operators”.
“Comparison Operators”