Check if string is in list of strings Check if string is in list of strings powershell powershell

Check if string is in list of strings


First and foremost, $MachineList = "srv-a*" -or "srv-b*" -or ... won't do what you apparently think it does. It's a boolean expression that evaluates to $true, because PowerShell interprets non-empty strings as $true in a boolean context. If you need to define a list of values, define a list of values:

$MachineList = "srv-a*", "srv-b*", ...

Also, the -contains operator does exact matches (meaning it checks if any of the values in the array is equal to the reference value). For wildcard matches you need a nested Where-Object filter

$MachineList = "srv-a*", "srv-b*", "srv-c*", ......if ($MachineList | Where-Object {$Machine.Name -like $_}) {    ...}

A better approach in this scenario would be a regular expression match, though, e.g.:

$pattern = '^srv-[a-l]'...if ($Machine.Name -match $pattern) {    ...}


use -eq for an exact match. use -match or -contains for a partial string match

$my_list='manager','coordinator','engineer', 'project engineer',$retval=$Falseif ($dd_and_pm -eq "engineer"){    $retval=$True}