robert
robert

Reputation: 3726

Check if a number belongs to a list

I want ot verify if the user entered number belongs to a predefined list, but the -contains operator behaves slightly different than I expected:

0 -contains 0.
True

The list is generated with the command (get-disk).number. What do I have to do to get False for the input 0.?

Upvotes: 1

Views: 788

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58961

First, you should wrap the output of (get-disk).number with @() to force the list to be an array. Then, ensure its an array of strings using a cast. Finally, pass the number you want to check using a string:

[string[]]$list = @((get-disk).number) # my list contains only 0

$list -contains "0." # False

The double 0. is eqal to the int 0 is equal to the string "0" thus you have to pass it as a string.

Upvotes: 2

Related Questions