How can you check whether input value is in array or not (Powershell)

Use the -contains operator:

$InputArray -contains $UserInput

With more recent PowerShell versions (v3 and above) you could also use the -in operator, which feels more natural to many people:

$UserInput -in $InputArray

Beware, though, that either of them does a linear search on the reference array ($InputArray). It doesn’t hurt if your array is small and you’re not doing a lot of comparisons, but if performance is an issue using hashtable lookups would be a better approach:

$validInputs = @{
    'a' = $true
    'e' = $true
    'i' = $true
    'o' = $true
    'u' = $true
    '1' = $true
    '2' = $true
    '3' = $true
    '4' = $true
    '5' = $true
}

$validInputs.ContainsKey($UserInput)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)