filter logic - exclude multiple

This topic contains 5 replies, has 4 voices, and was last updated by  John Curtiss 6 days, 12 hours ago.

  • Author
  • #100696

    John Curtiss

    get-aduser -filter {}

    I have some users who have 'xerox' in their givenname and some others who have 'xerox' in their surname. I want to exclude all of them from my search.

    the get-aduser filter won't let me use "notmatch." do I need to pipe to a where clause or is there a way to get the filter to work?

  • #100699


    For this, I would do

    Get-ADUser -filter {GivenName -notlike "Xerox" -or Surname -notlike "Xerox"}
    • #100710

      Olaf Soyk

      If I got it right you will have to change the "-or" to an "-and". 😉

    • #100711


      You're correct, that was my bad! Yes should be -and not -or

      Get-ADUser -filter {GivenName -notlike "Xerox" -and Surname -notlike "Xerox"}
  • #100716


    The only thing I would add to the previous responses is to use wildcards in your search string. The -like and -notlike operators look for an exact match, but it does accept * for a wildcard. I mention this because of your usage of the -notmatch operator. Match looks for patterns (and is capable of regex) where like is not.

  • #100741

    John Curtiss

    thanks everybody. for openers, i'm dumb and I assumed 'notlike' wasn't allowed either, because I think I've seen it not work in other filters where 'notmatch' wasn't allowed. didn't even try it.

    {GivenName -notlike "*Xerox*" -and Surname -notlike "Xerox*"} definitely does what I need, but when i look at it, it really looks like it should mean that 'Xerox' needs to appear in Both givenname -And surname for that filter to be true. for grins, what would the syntax be for that filter?

You must be logged in to reply to this topic.