Admin Tools

LDAP filter examples for Active Directory

22 filters admins reach for, ready to copy: what each one lists, the catch to know, and the AD Reports report that lists the same objects. Then the syntax — operators, the three matching-rule OIDs, escaping.

22 filters in 4 groups

What you type stays in your browser: nothing is sent anywhere.

Users14 filters

Each filter for user accounts starts with the same pair of conditions; the first one says why.

All user accounts

Every user account in the part of the directory you search.

(&(objectCategory=person)(objectClass=user))

Why both: (objectClass=user) alone also matches computer accounts, because the computer class is a subclass of user, and (objectCategory=person) alone also matches contacts.

In AD Reports, the report that lists the same objects: All Users Report.

Enabled user accounts

User accounts without ACCOUNTDISABLE (2) in userAccountControl.

(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))

Locked-out accounts are included: lockout is not a bit you can filter on (see Accounts with a lockout time).

In AD Reports, the report that lists the same objects: Enabled Including Locked User Accounts; Enabled User Accounts leaves the locked ones out.

Disabled user accounts

User accounts with ACCOUNTDISABLE (2).

(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))

In AD Reports, the report that lists the same objects: Disabled User Accounts.

Accounts with a lockout time

User accounts whose lockoutTime is not 0: locked out now, or locked out and not logged on since.

(&(objectCategory=person)(objectClass=user)(lockoutTime>=1))

Microsoft: lockoutTime is reset only by a successful logon, so an account whose lockout duration has passed is still listed; add the lockout duration to lockoutTime and compare with the current time to know. The LOCKOUT bit of userAccountControl does not help: since Windows Server 2003 that state is computed, not stored.

In AD Reports, Locked User Accounts lists only the accounts that are locked out.

Password never expires

User accounts with DONT_EXPIRE_PASSWORD (65536).

(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=65536))

In AD Reports, the report that lists the same objects: Password Never Expires.

Password not required

User accounts with PASSWD_NOTREQD (32): no password is required, so the password may be empty.

(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=32))

Interdomain trust accounts carry this flag by default (Microsoft); to leave them out, add (!(userAccountControl:1.2.840.113556.1.4.803:=2048)).

In AD Reports, the report that lists the same objects: Password Not Required; the security finding Accounts That Do Not Require a Password lists the enabled ones, trust accounts left out.

Must change password at next logon

User accounts whose pwdLastSet is 0 and whose password can expire.

(&(objectCategory=person)(objectClass=user)(pwdLastSet=0)(!(userAccountControl:1.2.840.113556.1.4.803:=65536)))

Microsoft: pwdLastSet 0 means the user must set a new password at the next logon only when userAccountControl lacks DONT_EXPIRE_PASSWORD (65536), hence the second condition.

In AD Reports, the report that lists the same objects: Must Change Password at Next Logon.

Accounts that never expire

User accounts whose accountExpires is 0 or 9223372036854775807.

(&(objectCategory=person)(objectClass=user)(|(accountExpires=0)(accountExpires=9223372036854775807)))

Both values mean never (Microsoft): a new account gets 9223372036854775807, and one whose expiry date is removed gets 0. The timestamp converter reads either.

In AD Reports, the report that lists the same objects: Accounts Never Expire.

Kerberos pre-authentication not required

User accounts with DONT_REQ_PREAUTH (4194304): the accounts AS-REP roasting targets.

(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=4194304))

In AD Reports, the report that lists the same objects: No Kerberos Preauthentication; the security finding AS-REP Roastable Accounts lists the enabled ones.

User accounts with a service principal name

User accounts with servicePrincipalName set: service accounts, and the accounts Kerberoasting targets.

(&(objectCategory=person)(objectClass=user)(servicePrincipalName=*))

(attribute=*) matches every object that has a value in the attribute; objectCategory=person keeps computer accounts out of the list.

In AD Reports, the report that lists the same objects: Users with SPNs.

adminCount = 1

User accounts that are, or once were, in a protected group such as Domain Admins: the system marked them and replaced their permissions with those of AdminSDHolder.

(&(objectCategory=person)(objectClass=user)(adminCount=1))

Removing an account from the group does not reset adminCount (Microsoft), so the list includes former administrators; group membership, not adminCount, decides who is protected now.

In AD Reports, the report that lists the same objects: All Protected Users; the security finding Orphaned adminCount Accounts and Groups lists those no longer in any protected group.

No logon for a number of days

User accounts that never logged on, or whose lastLogonTimestamp is older than midnight UTC that many days ago.

days
(&(objectCategory=person)(objectClass=user)(|(!(lastLogonTimestamp=*))(lastLogonTimestamp<=134317440000000000)))

lastLogonTimestamp runs 9 to 14 days behind with default settings, so choose well over 14 days; accounts that never logged on have no value, hence (!(lastLogonTimestamp=*)). For an exact date, make the value with the timestamp converter.

In AD Reports, the report that lists the same objects: Inactive Users for XX Days; on the Logon Timestamp page.

Created on or after a date

User accounts whose whenCreated is on or after midnight UTC of the day you pick.

(&(objectCategory=person)(objectClass=user)(whenCreated>=20260101000000.0Z))

whenCreated is generalized time, YYYYMMDDHHMMSS.0Z, in UTC. LDAP filters have no > or <: only >= and <=.

In AD Reports, the reports that list accounts created in a period: Accounts Created Between Specified Dates and Accounts Created During Last XX Days.

User accounts with an e-mail address

User accounts whose mail attribute is set.

(&(objectCategory=person)(objectClass=user)(mail=*))

mail holds the e-mail address set on the account. Exchange’s own view — mail users and mailboxes — is on the User Exchange reports page.

In AD Reports, the report that lists the same objects: Users with Email.

Groups4 filters

Group objects: (objectCategory=group).

Members of a group, nested groups included

Every object that is in the group, directly or through groups nested in it.

(memberOf:1.2.840.113556.1.4.1941:=CN=Helpdesk,OU=Groups,DC=example,DC=com)

Rule 1.2.840.113556.1.4.1941 follows the chain of memberships to the end. Not listed: accounts whose primary group this is (by default, Domain Users), because the group’s member attribute, which the rule follows, does not hold them (Microsoft). The other way round, (member:1.2.840.113556.1.4.1941:=<user DN>) lists every group a user is in. Special characters in the name are escaped for you.

In AD Reports, Everyone in… lists the same members, and also the accounts whose primary group it is.

Groups with no members

Groups with nothing in their member attribute.

(&(objectCategory=group)(!(member=*)))

A group can look empty here and still be the primary group of many accounts, as Domain Users is by default: primary group membership is not kept in member (Microsoft). Check before deleting.

In AD Reports, the report that lists the same objects: Groups Without Direct Members.

Security groups

Groups with the security flag, 0x80000000, in groupType.

(&(objectCategory=group)(groupType:1.2.840.113556.1.4.803:=2147483648))

Microsoft’s own example. The value must be decimal: 0x80000000 is 2147483648. Add a scope to narrow it — 2 global, 4 domain local, 8 universal — so 2147483650 lists global security groups and 2147483656 universal ones.

In AD Reports, the report that lists the same objects: All Security Groups.

Distribution groups

Groups without the security flag; Microsoft: a group without it is a distribution group.

(&(objectCategory=group)(!(groupType:1.2.840.113556.1.4.803:=2147483648)))

In AD Reports, the report that lists the same objects: All Distribution Groups.

Computers3 filters

Computer accounts: (objectCategory=computer).

Computers by operating system

Computer accounts whose operatingSystem begins with the name you enter.

(&(objectCategory=computer)(operatingSystem=Windows Server 2016*))

operatingSystem holds the edition too — Microsoft’s example is Windows Vista Enterprise — so the * at the end matches every edition. Special characters you type are escaped.

In AD Reports, the OS Computer Reports list each Windows version, for example Windows Server 2016.

Domain controllers

Writable domain controllers (SERVER_TRUST_ACCOUNT, 8192) and read-only ones (PARTIAL_SECRETS_ACCOUNT, 67108864).

(&(objectCategory=computer)(|(userAccountControl:1.2.840.113556.1.4.803:=8192)(userAccountControl:1.2.840.113556.1.4.803:=67108864)))

Microsoft gives a read-only domain controller’s computer object PARTIAL_SECRETS_ACCOUNT and WORKSTATION_TRUST_ACCOUNT, not SERVER_TRUST_ACCOUNT, so 8192 alone finds only the writable ones.

In AD Reports, the report that lists the same objects: Domain Controllers; Server Trust Accounts (Writable DCs) lists the writable ones.

Unconstrained delegation outside domain controllers

Enabled accounts trusted for delegation to any service (TRUSTED_FOR_DELEGATION, 524288), without the writable domain controllers, which carry the flag by default.

(&(userAccountControl:1.2.840.113556.1.4.803:=524288)(!(userAccountControl:1.2.840.113556.1.4.803:=8192))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))

Microsoft’s default for a domain controller is 532480: SERVER_TRUST_ACCOUNT plus TRUSTED_FOR_DELEGATION. A service running as one of the accounts listed can impersonate any client that uses it. No class in the filter: users and computers both.

In AD Reports, the report that lists the same objects: Unconstrained Delegation Outside Domain Controllers.

Contacts1 filter

Contact objects, with the pair of conditions Microsoft’s own examples use.

All contacts

Every contact object.

(&(objectCategory=person)(objectClass=contact))

In AD Reports, the report that lists the same objects: All Contacts.

Sources for the filters and their notes, Microsoft Learn: Search Filter Syntax · UserAccountControl property flags (KB 305144) · [MS-ADTS] userAccountControl Bits · [MS-ADTS] Read-Only Domain Controller Object · Computer class · Contact class · Lockout-Time · Pwd-Last-Set · Account-Expires · Service-Principal-Name · Admin-Count · Ask the Directory Services Team: Five common questions about AdminSdHolder and SDProp (2009, archived) · Last-Logon-Timestamp · “The LastLogonTimeStamp Attribute” (2009, archived) · When-Created · E-mail-Addresses (mail) · [MS-ADA2] Attribute member · Primary-Group-ID · Group-Type · Operating-System. The “In AD Reports” lines link this site’s Report Library.

Filter syntax

A filter is a condition in parentheses, (attribute operator value). Conditions combine with &, | and !, written in front of them: (&(a)(b)).

OperatorMeaningExample
=Equal to(sAMAccountName=jsmith)
~=Approximately equal to—
>=Greater than or equal to (Microsoft: lexicographically)(whenCreated>=20260101000000.0Z)
<=Less than or equal to (Microsoft: lexicographically)(lastLogonTimestamp<=134393814000000000)
&AND: every condition inside is true(&(objectCategory=group)(!(member=*)))
|OR: at least one is true(|(accountExpires=0)(accountExpires=9223372036854775807))
!NOT: the condition inside is false(!(userAccountControl:1.2.840.113556.1.4.803:=2))
  • Presence and wildcards. (attribute=*) matches objects that have the attribute and (!(attribute=*)) those without it; a * inside a value matches any characters, as in (cn=*smith*).
  • No > or <. The grammar has only >= and <=; for the opposite of one, put ! in front.

Matching rules

Written attribute:OID:=value, with no spaces, and the value as a decimal number — never hexadecimal, and never a constant name such as ADS_GROUP_TYPE_SECURITY_ENABLED.

OIDNameMatches whenExample
1.2.840.113556.1.4.803LDAP_MATCHING_RULE_BIT_ANDevery bit of the value is set in the attribute(userAccountControl:1.2.840.113556.1.4.803:=2) disabled accounts
1.2.840.113556.1.4.804LDAP_MATCHING_RULE_BIT_ORat least one bit of the value is set(userAccountControl:1.2.840.113556.1.4.804:=65568) password never expires or not required
1.2.840.113556.1.4.1941LDAP_MATCHING_RULE_IN_CHAINfor attributes that hold distinguished names only: following the chain of links, such as nested group membership, reaches the value(memberOf:1.2.840.113556.1.4.1941:=CN=Helpdesk,OU=Groups,DC=example,DC=com)

Microsoft notes that IN_CHAIN searches over a subtree can be processor-intensive where links fan out widely, such as listing every group a user is in.

Escaping special characters

These characters cannot appear as themselves in a value: write each as a backslash and its two hexadecimal digits.

CharacterWriteRequired by
*\2aRFC 4515 and Microsoft
(\28RFC 4515 and Microsoft
)\29RFC 4515 and Microsoft
\\5cRFC 4515 and Microsoft
NUL\00RFC 4515 and Microsoft
/\2fMicrosoft’s ADSI page; harmless elsewhere, since any byte may be written this way
  • Binary values are written byte by byte the same way: the four bytes 0x00000004 are \00\00\00\04.
  • Distinguished names. CN=Smith\, John (IT),OU=Staff,DC=example,DC=com goes into a filter as CN=Smith\5c, John \28IT\29,OU=Staff,DC=example,DC=com: its backslash and parentheses are escaped, the rest stays.

Escape a value

Escaped, for the filter

—

What you type stays in your browser: nothing is sent anywhere.

Sources: Microsoft Learn, Search Filter Syntax (operators, matching rules, special characters, binary values) · RFC 4515, String Representation of Search Filters (sections 3 and 4).

In AD Reports

Each filter above sits beside the report that lists the same objects. To run a filter of your own in AD Reports:

Run filters without writing them

Download the free 14-day trial: 560 ready-made reports, or paste your own filter into a report's LDAP Filter page.