Search Active Directory where the UPN contains numbers? RRS feed

  • Question

  • Right now I have the following search.fiter but it would be nice if I could get my search to return any results where there is a combination of numbers. I have noticed a few accounts in AD that have UPNs that are not correctly configured e.g. I want to find all of those that have numbers in them. There will be cases where there should be a number such as  So I'm thinking a something like in sql where you can query the value that contains more than 1 number.  so bob1 would return but bob11 would

    Im stumped on this, hoping for help!

    search.Filter = "(&(objectClass=User) (userPrincipalName=*" + username + "*))";''


    Friday, September 14, 2018 7:14 PM

All replies

  • No filter can count the number of digits, or even if any characters are digits. You need to retrieve all userPrincipalName values, then loop through the results with code to determine if more than one character is a digit.

    But looking at your filter example, maybe it would help to retrieve all UPNs that do not include the username. The "!" character is the "Not" operator in an LDAP syntax filter.

    search.Filter = "(&(objectClass=User) (!(userPrincipalName=*" + username + "*)))";''

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Saturday, September 15, 2018 9:28 PM