The Saved Queries in Active Directory Users and Computers (ADUC) console allow to create simple and complex LDAP queries by samples of Active Directory objects. These queries can be saved, edited and transferred to other computers. Using the Saved Queries, you can deal with search and sampling of AD objects by different criteria quickly and effectively. For example, Saved Queries can help you quickly: display the list of all disabled accounts in a domain, select all users of a company who have mailboxes on a given Exchange server, etc.
An important benefit of the saved LDAP queries is the opportunity to perform group operations with the objects from different OUs in Active Directory, like bulk locking/unlocking, moving, deleting of accounts, etc. It allows to get rid of some faults in the hierarchical OU structure in Active Directory by collecting all necessary objects in a flat table view.
The majority of these operations can be done using PowerShell, dsquery, vbs scripts, etc., but as a rule, it is more convenient to have the results displayed in the familiar graphic console view and doesn’t require any special skills.
Active Directory Saved Queries first appeared in Windows Server 2003 and got further support in the later Windows Server versions.
Let’s consider a typical example of using the Saved Queries in Active Directory Users and Computers. Suppose, we have to display the list of active user accounts, their departments and e-mail addresses.
Open ADUC (dsa.msc), right-click Saved Queries and select New – > Query.
In the Name box, specify the name of the saved query to be displayed in the ADUC console.
In Query root box, you can specify the container (OU), in which the query is run. By default, the search by the query criteria is performed in the whole AD domain. In our example, we’ll narrow the search scope by selecting Brasil container.
Then click Define Query, and select Custom Search on Find drop down.
Go to Advanced tab and copy the following LDAP query into Enter LDAP query box:
Save the changes by clicking OK.
Select the created query in ADUC console, press F5 to rebuild the list. The result of the query is displayed on the next screenshot.
To display the additional fields (e-mail address, department, etc.) open View menu in ADUC console and select Add/Remove Columns.
Add the necessary fields. We have added 3 additional fields: User Logon Name, E-Mail Address, Department.
This result can be downloaded as a CSV or TXT file for further analysis and viewed in Excel. To do it, right-click the saved query and select Export List.
In ADUC console, you can create a number of different saved queries to be arranged into a tree structure.
The saved queries are stored locally in the console on the computer on which they were created. (An XML file containing the settings can be found here: C:\Users\%USERNAME%\AppData\Roaming\Microsoft\MMC\DSA) To move a saved query from one computer to another, there is a feature to import/export the queries as XML files in dsa.msc.
In the next table we give some examples of commonly used LDAP queries that can be selected in Active Directory.
|Search of the groups with ‘admin’ keyword in the name|
|Search of the accounts with ‘service’ keyword in description box|
|Empty Active Directory groups (with no users)|
|Users with the setting “Password never expires”|
|Users with the empty Profile Path box|
|Active accounts of the users that must change password|
|All AD users, except disabled|
|Blocked AD users|
|Users with e-mail addresses|
|Users without e-mail addresses|
|Computers running Windows XP SP3|
|The list of accounts never registered in the domain (the information on time of the logon to the domain can be obtained in a more convenient view in Additional Account Info tab)|
|User accounts created at a certain time period (in 2014)|
|AD users created this year|