Home > Back-end >  How to use multiple values to compare in powershell
How to use multiple values to compare in powershell

Time:10-27

In simple words I am trying -

$animal = "cat", "dog", "rabbit"
$animal -contains "dog" # -----> Getting true, which is working!

However, when I try to check multiple values like -

$animal -contains "dog" -and "bat" # --> still getting a "True" response. 

How can I check multiple values using contains operator?

CodePudding user response:

Boolean expressions work a little differently. You need this:

$animal -contains "dog" -and $animal -contains "bat"

You could also put "dog" and "bat" in their own array, and loop through the array.

CodePudding user response:

To complement Joel Coehoorn's helpful answer, you can use the IsSupersetOf(IEnumerable<T>) Method from the HashSet<T> Class.

$animal = [System.Collections.Generic.HashSet[string]]::new(
    [string[]] ('cat', 'dog', 'rabbit'),
    [System.StringComparer]::OrdinalIgnoreCase
)

$animal.IsSupersetOf([string[]] ('dog', 'bat')) # => False
$animal.IsSupersetOf([string[]] ('dog', 'cat')) # => True
  • Related