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.
LDAP (Lightweight Directory Access Protocol) queries are used to retrieve info from directory services. Microsoft Active Directory Domain Services (AD DS) supports LDAP, allowing admins and apps to search for users, computers, groups, and other directory objects based on specific criteria.
You can perform LDAP searches against AD using various tools, including:
Saved Queries in the Active Directory Users and Computers (ADUC) MMC console
PowerShell Active Directory cmdlets with the -LDAPFilter parameter
ldp.exe, Microsoft’s graphical LDAP client included with Windows and Windows Server
Legacy command-line tools such as dsquery.exe and dsget.exe
ldapsearch, a separate command-line LDAP client that is not included with Windows by default
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:
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. ADSI Edit uses the standard LDAP filter syntax. When you need to combine multiple conditions with a logical AND, use the & operator, for example: (&(condition1)(condition2))
Executing LDAP Queries in PowerShell
You can use PowerShell to run an LDAP query against Active Directory. Most cmdlets from 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:
Note. You should not rely on the operatingSystem attribute alone to distinguish Windows 10 from Windows 11. Some Windows 11 systems can report an operatingSystem value beginning with Windows 10. When you need to distinguish Windows client versions, check the operatingSystemVersion attribute and the Windows build number. Windows 11 starts with build 22000.
Note that this is a substring search. The leading wildcard prevents a prefix match and can be expensive on large directories. When possible, you should prefer a prefix search such as (cn=Brion*) or use a more selective filter.
Our example below first locates the built-in Domain Admins group by its well-known RID (512) and then uses the group’s distinguished name in an LDAP filter to find users who are direct/transitive members of the group:
function Escape-LdapFilterValue { param( [Parameter(Mandatory)] [string]$Value )
Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=$dn))" | Select-Object DistinguishedName, Name
Security note. The DistinguishedName returned by AD is escaped according to DN syntax, but a DN inserted into an LDAP filter is being used as a filter value and may require additional LDAP filter escaping. DN escaping and LDAP filter escaping are different.
Our example above uses the fixed Domain Admins group, whose DN does not contain characters that cause a problem in this filter. However, you should not assume that every DN can be inserted into an LDAP filter unchanged. A group name such as Sales (EU) can produce a DN containing parentheses, which have special meaning in LDAP filter syntax.
LDAP filter values must escape special characters such as *, (, ), \, and NUL. Their hexadecimal escapes are \2a, \28, \29, \5c, and \00, respectively.
In case a filter value comes from user input/another external source, you should always escape it before inserting it into the filter. Avoid constructing LDAP filters by concatenating untrusted values.
Using DSQUERY and DSGET with LDAP Filters
You can run LDAP queries against Active Directory using the built-in Windows command prompt tools such as dsget.exe and dsquery.exe.
For example, to find all users whose job title starts with Manager, run the command:
dsquery * "OU=Employees,DC=theitbros,DC=com" ^ -filter "(&(objectCategory=person)(objectClass=user)(title=Manager*))" ^ -attr displayName title
You can use the dsget.exe command to retrieve specific attributes from an AD object. Unlike dsquery, which is primarily used to find objects, dsget displays selected properties of an object. For example, to display the name and email address of a user, use the following command:
dsget user "CN=John Smith,OU=Users,DC=theitbros,DC=com" -display -email
The dsget command works with the distinguished name (DN) of the object and can display different attributes depending on the object type and the parameters you specify.
Using ldapsearch to Query Active Directory
ldapsearch is a command-line LDAP client commonly used on Linux and other Unix-like systems. It is not a standard built-in Windows LDAP query tool; on Windows, it can be used through environments/packages that provide OpenLDAP utilities. It allows you to send LDAP search requests directly to an Active Directory DC. For example, the following command searches for AD users whose Department attribute is set to Sales:
Note that you should use a least-privileged account for simple LDAP binds; do not use a Domain Admin account for read-only queries.
The main parameters used in this example are:
-x — use simple authentication instead of SASL;
-H — specify the LDAP server URI;
-D — specify the bind account used to authenticate to AD;
-W — prompt for the bind account password;
-b — specify the base distinguished name (Base DN) from which to search;
the LDAP filter — specify the conditions that objects must match;
sAMAccountName displayName mail — specify the attributes to return for matching objects.
Important. Windows Server 2025 introduces stronger default LDAP security for new AD deployments. LDAP signing is required by default on newly deployed DCs. As a result, unsigned LDAP binds can fail out of the box on a new Windows Server 2025 AD DS deployment.
Note that this default does not necessarily apply to domains upgraded from earlier Windows Server versions. When upgrading existing DCs to Windows Server 2025, existing LDAP signing policies are preserved to avoid compatibility issues.
LDAP channel binding is also supported and is configured as When supported by default for new Windows Server 2025 deployments. Windows Server 2025 provides additional auditing for channel binding so admins can identify clients that do not support/fail channel binding before moving to stricter enforcement.
You should not rely on unsigned LDAP connections. Use LDAP signing for SASL/Kerberos/NTLM LDAP connections and use LDAPS (ldaps://) when TLS encryption is required. Before enforcing stricter LDAP security policies, you need to identify legacy apps and devices that may not support LDAP signing/channel binding.
Relevant Directory Service events include LDAP signing events 2886–2889 and LDAP channel-binding events 3039–3041. Windows Server 2025 also supports channel-binding audit events 3074 and 3075 for identifying clients that may be affected by a stricter channel-binding policy.
You should avoid sending credentials directly in the command line for production environments. You can use -W to prompt for the password instead of including it in the command.
LDAP Query Syntax and Operators
The common LDAP filter syntax is:
(<attribute><operator><value>)
Hint. LDAP attribute names are case-insensitive. LDAP search filters support Boolean operators such as &, |, and !, as well as extensible matching rules.
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
Approximate match in LDAP; in AD it behaves like =
Important. Although ~= is defined as an approximate match in the LDAP filter syntax, AD does not implement a true fuzzy/approximate comparison for this operator. In AD, ~= behaves like the equality operator =. You should not use it to find values that are merely similar to the specified value.
For example, the following filter find objects whose Common Name is exactly 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, if the previous filter was (cn=Jon), change it to (cn=Jo*) to match cn values that start with Jo:
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:
Using Ambiguous Name Resolution (ANR) in LDAP Filters
AD supports Ambiguous Name Resolution (ANR), which allows a single search value to be matched against a set of attributes configured for ANR in the directory schema. For example:
(anr=Denis)
ANR is intended for name-like lookups rather than as a general-purpose substring search across arbitrary attributes. In a typical AD schema, ANR can consider attributes such as displayName, givenName, sn, sAMAccountName, the RDN, legacyExchangeDN, proxyAddresses, physicalDeliveryOfficeName, and msDS-AdditionalSamAccountName. The exact set depends on the schema and ANR config; Exchange can also add/modify ANR-related schema behavior.
Avoid adding leading and trailing wildcards such as:
(anr=*Denis*)
when a prefix-style ANR lookup is sufficient. The wildcard changes the search into a substring-style condition and can require substantially more directory work on large AD environments. If you specifically need substring matching, you should query the appropriate attribute directly and use a wildcard only when that search behavior is actually required.
This ANR filter searches for objects whose ANR-enabled attributes match Denis. Note that it is not limited to user objects unless the filter also includes user-specific conditions such as (objectCategory=person)(objectClass=user).
Note. AD domain controllers limit the number of objects returned in a single LDAP search response by the MaxPageSize directory setting, which is commonly 1000 objects. PowerShell AD cmdlets (such as Get-ADUser) use paged searches, so this limit does not normally prevent them from returning more than 1000 objects. The -ResultPageSize parameter controls the number of objects requested per page, while -ResultSetSize controls the total number of objects returned.
Let’s look at some useful examples of LDAP queries commonly used by AD admins.
Find Accounts with adminCount=1
In order to find user accounts with adminCount=1, use the following:
(adminCount=1)
Note that you should not use adminCount=1 as a definitive test for current membership in privileged groups. An account may retain this value after it is removed from a protected group. To audit current privileged access, you need to query the membership of the relevant protected groups directly.
lastLogonTimestamp is stored as a Windows FILETIME value. Unlike lastLogon, it is replicated between DCs, and this makes it useful for identifying inactive accounts, but it is not updated on every logon. Therefore, you should treat it as an approximate activity indicator rather than an exact last-logon timestamp. Here is an example:
The lockoutTime attribute records the time when an account was locked. A non-zero value does not necessarily mean that the account is currently locked, because the value can remain after the account is automatically unlocked. To find accounts with a non-zero lockoutTime, use the command:
For an authoritative current list of locked-out accounts, you should use Search-ADAccount -LockedOut rather than treating lockoutTime>=1 as a current lockout flag:
Important. This filter does not reliably identify Windows 10 devices. Some Windows 11 systems can report an operatingSystem value beginning with Windows 10. You should use operatingSystemVersion/build info or an endpoint-management inventory to distinguish Windows 10 from Windows 11. LDAP ordering comparisons on operatingSystemVersion are not numeric build comparisons.
List Domain Controllers and Servers
To find writable domain controllers, use the following:
The following LDAP filter finds groups that have no direct members stored in the member attribute. Note that primary group membership is not represented by the member attribute, so this filter should not be treated as a complete test for whether a group has no members.
(&(objectCategory=group)(!(member=*)))
List the membership of groups, including nested AD groups:
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.
ANR allows partial searches across multiple attributes (like displayName, mail, or sAMAccountName). Example — find users containing “Denis” in any of those fields:
I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
You can even use wildcards but the following can take a long time: (Get-ADObject -LdapFilter “(CN=*)”).DistinguishedName.ForEach({ $_; Get-ADObject -LdapFilter “(&(objectclass=user)(MemberOf=$_))”})
Bart van den Donk
5 years ago
For better output (Group CN can even contain wildcard characters): (Get-ADObject -LdapFilter “(&(objectclass=group)(CN=Group CN))”) | ForEach-Object {$a=$_.Name; Get-ADObject -LdapFilter “(&(objectclass=user)(MemberOf=$($_.DistinguishedName)))” | Select-Object DistinguishedName, Name, @{l=’GroupName’;e={$a}}}
deepak
4 years ago
how to get attributes value in ad through LDAP
CC
4 years ago
I want a list of members in an AD computer group. I’m not having any success in finding the right cmd or script to run an AD query to list members of a computer group. It seems to work only for user accounts. Any advice is greatly appreciated
Get-ADObject -LdapFilter “(&(objectclass=user)(MemberOf=$((Get-ADObject -LdapFilter “(CN=)”).DistinguishedName)))”
(Get-ADObject -LdapFilter “(CN=Group CN)”).DistinguishedName.ForEach({ $_; Get-ADObject -LdapFilter “(&(objectclass=user)(MemberOf=$_))”})
You can even use wildcards but the following can take a long time:
(Get-ADObject -LdapFilter “(CN=*)”).DistinguishedName.ForEach({ $_; Get-ADObject -LdapFilter “(&(objectclass=user)(MemberOf=$_))”})
For better output (Group CN can even contain wildcard characters):
(Get-ADObject -LdapFilter “(&(objectclass=group)(CN=Group CN))”) |
ForEach-Object {$a=$_.Name;
Get-ADObject -LdapFilter “(&(objectclass=user)(MemberOf=$($_.DistinguishedName)))” |
Select-Object DistinguishedName, Name, @{l=’GroupName’;e={$a}}}
how to get attributes value in ad through LDAP
I want a list of members in an AD computer group. I’m not having any success in finding the right cmd or script to run an AD query to list members of a computer group. It seems to work only for user accounts. Any advice is greatly appreciated
Example:
Get-ADGroupMember “GroupName”
How would I run a search for accounts that have the same display name in two different, but trusted, domains?
1. I want list of all dsitribution list in “Groups” OU along with the the users in each group printable in csv file.
2. If it is possible to to include groups that have GroupType as Security Group as well but only those that has email address defined.
2nd filter not required but if added would be great help