LDAP syntax filters can be used in many situations to query Active Directory. They can be used in VBScriptand PowerShell scripts. Many utilities, like adfind and dsquery *, accept LDAP filters. Many PowerShell Active Directory module cmdlets, like Get-ADUser, Get-ADGroup, Get-ADComputer, and Get-ADObject, accept LDAP filters with the LDAPFilter parameter.
Table of Contents
LDAP Clauses
A filter specifies the conditions that must be met for a record to be included in the recordset (or collection) that results from a query. An LDAP filter has one or more clauses, each enclosed in parentheses. Each clause evaluates to either True or False. An LDAP syntax filter clause is in the following form:
(<AD Attribute><comparison operator><value>)
The <AD Attribute> must the the LDAP Display name of an Active Directory attribute. The allowed comparison operators are as follows:
Operator | Meaning |
= | Equality |
>= | Greater than or equal to (lexicographical) |
<= | Less than or equal to (lexicographical) |
Note that the operators "<" and ">" are not supported. Another operator, ~= (which means approximately equal to) is supported, but no case has been found where this is useful in Active Directory. The <value> in a clause will be the actual value of the Active Directory attribute. The value is not case sensitive and should not be quoted. The wildcard character "*" is allowed, except when the <AD Attribute> is a DN attribute. Examples of DN attributes aredistinguishedName, manager, directReports, member, and memberOf. If the attribute is DN, then only the equality operator is allowed and you must specify the full distinguished name for the value (or the "*" character for all objects with any value for the attribute). Do not enclose the DN value in parentheses (as is done erroneously in some documentation). If the attribute is multi-valued, then the condition is met if any of the values in the attribute match the filter. An example LDAP syntax filter clause is:
(cn=Jim Smith)
This filters on all objects where the value of the cn attribute (the common name of the object) is equal to the string"Jim Smith" (not case sensitive). Filter clauses can be combined using the following operators:
Operator | Meaning |
& | AND, all conditions must be met |
| | OR, any of the conditions must be met |
! | NOT, the clause must evaluate to False |
For example, the following specifies that either the cn attribute must be "Jim Smith", or the givenName attribute must be "Jim" and the sn attribute must be "Smith":
(|(cn=Jim Smith)(&(givenName=Jim)(sn=Smith)))
Conditions can be nested with parentheses, but make sure the parentheses match up.
Special Characters
The LDAP filter specification assigns special meaning to the following characters:
* ( ) NUL
The NUL character is ASCII 00. In LDAP filters these 5 characters should be escaped with the backslash escape character, followed by the two character ASCII hexadecimal representation of the character. The following table documents this:
Character | Hex Representation |
* | 2A |
( | 28 |
) | 29 |
5C | |
Nul |