PowerShell Where-Object like multiple string values

Viewed 58115

I'm using the following in a do-until block to loop until a specified Exchange Online migration status is present:

(Get-Migrationbatch -Identity $MigrationBatchName | Where {$_.Status -like "Completed" -or "CompletedWithErrors" -or "Corrupted" -or "Failed" -or "Stopped"})

However, the above still returns a job with the status of "Syncing" and so continues the script regardless.

I've tried -match, -eq but still the same.

What am I missing?

3 Answers

It would be simpler using -in operator, given that you are not using wildcards:

(Get-Migrationbatch -Identity $MigrationBatchName | Where Status -in "Completed","CompletedWithErrors","Corrupted","Failed","Stopped")

another option convert filter array to regex string...

$filter_status = @("Completed", "CompletedWithErrors","Curropted","Failed", "Stopped")
(Get-Migrationbatch -Identity $MigrationBatchName | Where Status -Match ($filter_status -Join "|")
Related