using substitute for "-in" in powershell 2.0

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Shane Lindberg Shane Lindberg 1 year, 6 months ago.

  • Author
    Posts
  • #30088
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    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}
    }
    
  • #30091
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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
    
  • #30092
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    Thanks! much cleaner 🙂

You must be logged in to reply to this topic.