Author Posts

September 24, 2015 at 5:44 pm

I have a script I wrote using -in and -notin what does not run in powershell 2.0

#this works in powershell 3.0+

#I want to test $i is in (1,2,3)
$i = 1
$a = ($i -in (1,2,3))


#what would be the simplest way to do that in powsershell 2.0
#I came up with the following, but wondered if there is a much
#easier way to do it


$a = $false
$i = 1
switch ($i){
    1{$a = $true}
    2{$a = $true}
    3{$a = $true}
}

September 24, 2015 at 5:59 pm

Use -contains

PS C:\> $i = 1
PS C:\> $a = (1,2,3) -contains $i
PS C:\> $a
True
PS C:\> $a = (1,2,3) -contains 4
PS C:\> $a
False

September 24, 2015 at 6:00 pm

Thanks! much cleaner 🙂