Using PrincipalSearcher to find users matching pattern

114 Views Asked by At

I have a somewhat particular situation I couldn't find answers to through google.

I need to query my active directory for users whose SAMAccountName fits a particular pattern (3 letters followed by 4 digits).

Currently I pull the entire list of users and filter it with regex client-side.

Can this somehow be done directly in the query so that only users with that matching field are returned from the server in the first place?

1

There are 1 best solutions below

0
On

Unfortunately, the best you can do is limit it to accounts where the first character is a letter.

The only operators that could help are >= and <=. For example, you can have a filter like this:

(&(sAMAccountName>=A*)(sAMAccountName<=Z*))

That will exclude accounts that start with anything other than a letter. But there is no way to extend that to any more than just the first character.

I'm guessing that probably isn't going to help.

More reading: Active Directory: LDAP Syntax Filters