filter logic - exclude multiple

Welcome Forums General PowerShell Q&A filter logic - exclude multiple

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
6 months ago.

  • Author
    Posts
  • #100696

    Participant
    Points: 18
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

    For this, I would do

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

      Participant
      Points: 144
      Helping Hand
      Rank: Participant

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

    • #100711

      Participant
      Points: 21
      Rank: Member

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

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

    Participant
    Points: 58
    Rank: Member

    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

    Participant
    Points: 18
    Rank: Member

    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?

The topic ‘filter logic - exclude multiple’ is closed to new replies.