none
Using an -or statement to filter out values on one attribute RRS feed

  • Question

  • Hi,

    Given the following Powershell commands in Exchange:

    get-mailboxdatabase | where{$_.name -notlike "NYC*"}

    get-mailboxdatabase | where{$_.name -notlike "LAX*"}

    ..correctly and individually gives me a list of all databases that do not start with NYC or LAX.

    However, I am trying to get a list of databases that do not start with either NYC or LAX and using either of the following commands:

    get-mailboxdatabase | where{($_.name -notlike "NYC*") -or ($_.name -notlike "LAX*")}

    get-mailboxdatabase | where{$_.name -notlike "NYC*" -or $_.name -notlike "LAX*"}

    ...I get a complete list of databases including NYC and LAX.

    What might I be doing incorrectly?

    Thank you in advance!
    JC

    Friday, May 22, 2015 12:44 PM

Answers

  • You should be using -and, not -or.


    With the -or switch, only one side needs to be true, seeing as a database can't start with both NYC and LAX then one of the two comparisons will always be true.  With the -and switch, both sides need to be true so it should filter as you want.
    • Edited by kdcooke Friday, May 22, 2015 1:14 PM
    • Marked as answer by Jupooka Friday, May 22, 2015 1:16 PM
    Friday, May 22, 2015 1:08 PM

All replies

  • You should be using -and, not -or.


    With the -or switch, only one side needs to be true, seeing as a database can't start with both NYC and LAX then one of the two comparisons will always be true.  With the -and switch, both sides need to be true so it should filter as you want.
    • Edited by kdcooke Friday, May 22, 2015 1:14 PM
    • Marked as answer by Jupooka Friday, May 22, 2015 1:16 PM
    Friday, May 22, 2015 1:08 PM
  • Aha!  There it was all along right in front of me.  Thanks!  That worked.  I'll have to be sure to rethink my -and/or logic next time.

    I appreciate the response!

    JC

    • Marked as answer by Jupooka Friday, May 22, 2015 1:16 PM
    • Unmarked as answer by Jupooka Friday, May 22, 2015 1:16 PM
    Friday, May 22, 2015 1:16 PM