How to use Powershell Where-Object like an IN statement
Use the -contains
operator. Like:
$dbs = "testDB", "master", "model", "msdb"foreach ($db in ($svr.Databases | where-object {$dbs -contains $_.name } )) { write-output $db.name}
Use help about_Comparison_Operators
to learn more about this and other comparison operators.
Update:
PowerShell v3 has added the -in
operator. The example in the original question will work in v3.
You can use a regex:
$svr.Databases | where { $_.name -match 'testDB|master|model|msdb' } | foreach { $db.name }
you can do this (at least in PSv5):
$srv.Databases | where Name -in "testDB","master","model","msdb" | write-output { $_.Name }