ObjSearcher using -like on a variable

Welcome Forums General PowerShell Q&A ObjSearcher using -like on a variable

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

3 years, 1 month ago.

  • Author
  • #33054

    Points: 79
    Rank: Member

    I have some working code that looks like this:
    $strFilter = "(&(objectCategory=User)("+$SCriteria+"="+$SValue+"))"
    $objSearcher.Filter = $strFilter

    using this in some ad searching functionality, what I'm trying to do is on the second = in strfilter IE the criteria matching supplied value, to change to -like
    most common use on this piece is searching ad accounts for an employee id field, where users can have multiple accounts, the primary having a letter preceeding a string of digits, and the secondary account only has the digits.
    right now I can only search on specific values, I need to be able to do a -like $SValue

    I know this is a bit rambling but any assistance would be appreciated

  • #33055

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    That filter is processed by AD, not PowerShell, and so -like (or any other shell operator) isn't valid. But * is a valid wildcard; see if http://stackoverflow.com/questions/9564120/using-wildcards-in-ldap-search-filters-queries doesn't help.

  • #33056

    Points: 79
    Rank: Member

    thanks Don, was trying to edit someone else's code, not really sure why he had the + preceeding and trailing the values...

    but managed to have this work:
    $strFilter = "(&(objectCategory=User)($SCriteria=*$SValue))"


The topic ‘ObjSearcher using -like on a variable’ is closed to new replies.