In this article, we’ll explain how to use LDAP queries to retrieve information about users, computers, and groups from the Active Directory domain using PowerShell, ADUC, and command prompt tools. It includes the syntax of LDAP search filters, operators, and practical LDAP query examples for AD.
Contents
Toggle
What Is an LDAP Query in Active Directory
How to Run LDAP Queries Against Active Directory
Using Saved Queries in Active Directory Users and Computers (ADUC)
Running LDAP Filters in ADSI Edit Console
Executing LDAP Queries in PowerShell
Using DSQUERY and DSGET with LDAP Filters
LDAP Query Syntax and Operators
Comparison Operators (=, >=, <=, ~=)
Boolean Operators (AND, OR, NOT)
Using objectClass and objectCategory Attributes
Using Wildcards in LDAP Search Filters
Using Ambiguous Name Resolution (ANR) in LDAP Filters
LDAP Query Examples for Active Directory Users
Find Disabled or Locked AD Accounts
List Users with Password Never Expires
Search Users by Department or Group Membership
Search Users by Location
LDAP Query Examples for Computers in Active Directory
Find Disabled Computers
Search Computers by Operation System:
List Domain Controllers and Servers
LDAP Query Examples for Active Directory Groups
Find Empty AD Groups
List Distribution Groups
What is an LDAP query in Active Directory?
What tools can I use to run LDAP queries in Active Directory?
What is the syntax of an LDAP query?
Which operators can be used in LDAP filters?
How do I use LDAP queries in PowerShell?
How can I list or filter Active Directory groups using LDAP queries?
How can I search for computers using LDAP filters?
What is Ambiguous Name Resolution (ANR) in LDAP?
What Is an LDAP Query in Active Directory
LDAP (Lightweight Directory Access Protocol) queries are used to retrieve information from various directory services. Microsoft’s Active Directory Domain Service (AD DS) is a directory service that also supports the LDAP protocol. Users can perform LDAP queries against Active Directory to search (filter) for computers, users, groups, and other objects based on specific criteria using various tools, like:
Saved Queries in the Active Directory Users and Computers MMC console GUI
PowerShell cmdlets
ldapsearch.exe, dsget.exe, and dsquery.exe commands
Here is an example of an LDAP query that can be used to find Active Directory users with the “User must change password at next logon” option enabled:
This LDAP query contains several conditions, each of which is enclosed in brackets.
How to Run LDAP Queries Against Active Directory
Now, let’s review the most popular methods for running an LDAP query against Active Directory.
Using Saved Queries in Active Directory Users and Computers (ADUC)
The Active Directory Users and Computers MMC snap-in offers a graphical interface for executing an LDAP filter to search for AD objects.
Open the ADUC console (run dsa.msc) and go to the Saved Queries;
Create a new query;
Specify a query name and click the Define Query button;
Select the Custom Search type. Go to the Advanced tab, and paste your LDAP query code into the Enter LDAP query field;
Save the query and press F5;
A list of AD users matching this LDAP query should appear in the right pane.
Running LDAP Filters in ADSI Edit Console
You can also use LDAP filters when searching for objects in the ADSI Edit console.
Right-click on the naming context and select New > Query;
Specify the query name;
Select the search area (Root of Search). Paste your LDAP query code into the Query String field.
Note. An LDAP query must be converted to the following format in order to be used in the ADSI Edit console: (&your_ldap_filter).
Executing LDAP Queries in PowerShell
You can use PowerShell to run an LDAP query against Active Directory. Most cmdlets from the the PowerShell Active Directory module (like Get-ADUser, Get-ADComputer, Get-ADGroup, Get-ADObject, etc.) include an LdapFilter parameter that allows you to specify your LDAP query.
For example, use the Get-ADUser cmdlet with an LDAP filter to search for the user objects that match these criteria:
The Get-ADComputer cmdlet is used to search for computer objects:
Get-ADComputer –LDAPFilter ‘your ldap query’
Below is an example of a complex LDAP filter with multiple OR conditions to search for Windows 10 and 11 computers whose hostnames don’t contain the keywords WK and TEST:
Hint. LDAP query attributes are not case sensitive.
Comparison Operators (=, >=, <=, ~=)
The following comparison operators can be used in a filter:
Operator
Syntax
Description
=
attribute=value
Equal
>=
attribute>=value
More or equal
<=
attribute<=value
Less or equal
~=
attribute~=value
Approximately equal to
For example, the following filter returns all objects with cn (common name) attribute value Jon:
(cn=Jon)
Boolean Operators (AND, OR, NOT)
Boolean operators allow you to specify multiple search conditions:
Operator
Syntax
Description
&
(&(filter1) (filter2))
AND — all conditions must be met
|
(|(filter1) (filter2))
OR — any number of conditions can be met
!
(!(filter1))
NOT — the condition must not be met
For example, let’s find AD objects with cn=Jon AND sn (surname)= Brion:
(&(cn=Jon)(sn=Brion))
You can use several logical operators in one filter. The following LDAP query returns objects with cn = Jon OR sn = Brion, for which cn is not equal to Alex:
(&(|(cn=Jon)(sn=Brion)(!(cn=Alex)))
Using objectClass and objectCategory Attributes
The objectCategory and objectClass attributes allow you to refine the search objects.
If you don’t know the exact name of the object, you can use the asterisk (*) wildcard character in the LDAP filter. For example, the previous query to find users whose names start with Jo would need to be changed to:
A wildcard in LDAP query can be used to check if a particular attribute of an object has any value. For example, to find all users who have the mail attribute filled in, use this filter:
Sometimes, it is useful to apply the comparison operators “≥” and “≤” instead of a wildcard. For example, this filter displays users whose names start with the letter X and continue in alphabetical order (i.e., X, Y, or Z).
Using Ambiguous Name Resolution (ANR) in LDAP Filters
The Ambiguous Name Resolution (AMR) feature can be used in an LDAP filter to simplify searches and queries for users or contacts in AD whose names are only partially known. For example, the LDAP (anr=*Denis*) will filter objects containing Denis in any of the following user attributes:
displayName
sAMAccountName
Relative Distinguished Name (RDN),
givenName
sn
legacyExchangeDN
msExchResourceSearchProperties
proxyAddresses
mailNickname
mail
physicalDeliveryOfficeName
This filter will return all users with at least one attribute from the list that matches your string.
LDAP Query Examples for Active Directory Users
Let’s look at some useful examples of LDAP queries commonly used by AD admins.
Find Disabled or Locked AD Accounts
Search for users in privileged groups (Domain Admins, Enterprise Admins, etc):
An LDAP (Lightweight Directory Access Protocol) query is used to search and retrieve objects from a directory service such as Active Directory. You can use it to find users, computers, groups, and other objects based on specific criteria.
What tools can I use to run LDAP queries in Active Directory?
LDAP queries can be executed using several tools, including:
Active Directory Users and Computers (ADUC) via Saved Queries
ADSI Edit console
PowerShell cmdlets like Get-ADUser, Get-ADComputer, Get-ADGroup, and Get-ADObject
Command-line tools such as dsquery.exe and dsget.exe
What is the syntax of an LDAP query?
The general syntax is:
<filter> = (<attribute><operator><value>)
Example: (cn=John)
Which operators can be used in LDAP filters?
= (equal)
>= (greater or equal)
<= (less or equal)
~= (approximately equal)You can also use Boolean operators: & (AND), | (OR), ! (NOT).
How do I use LDAP queries in PowerShell?
Use the -LDAPFilter parameter in AD module cmdlets.
Example to find users who must change their password at next logon:
ANR allows partial searches across multiple attributes (like displayName, mail, or sAMAccountName).Example — find users containing “Denis” in any of those fields: