Home > Articles > Microsoft > MCTS

Retrieving Information from Active Directory with Dsquery and Dsget

  • Print
  • + Share This
Dsquery and dsget are powerful commands you can use to retrieve information from Active Directory. This article first shows you how to build a distinguished name (DN) and then how to use the DN within these commands. This article shows you how you can use dsquery and dsget to retrieve lists of users, computers, groups, inactive accounts, disabled accounts, accounts with stale passwords, and group memberships.
Like this article? We recommend

Dsquery and dsget are powerful commands you can use to retrieve information from Active Directory. For example, you can use them to retrieve a list of users, groups, inactive accounts, accounts with stale passwords, disabled accounts, group memberships, and more. The basic syntax of dsquery and dsget is as follows:

dsquery object DN -switch
dsget object DN -switch

There are several different types of Active Directory objects you can query, but for this topic, I’m limiting the discussion to users, computers, and groups. The distinguished name (DN) is a critical component of the command so it’s important to be able to build a DN for different objects. If you can build a DN, you can use this knowledge with several directory service (DS) commands including dsquery, dsget, dsmod, dsmove, and dsrm. Dsquery and dsget both supports many different switches and this article shows the usage for the inactive, disabled, stalepwd, members, and memberof switches.

Building Distinguished Names

Every object within Active Directory Domain Services (AD DS) is uniquely identified with a DN. You can use the DN to identify the domain, an Organizational Unit (OU) within the domain, and any object within the domain.

The three primary identifiers used in DNs with DS commands are

  • OU—Organizational Unit
  • CN—Common Name
  • CN can indicate an object such as a user, computer, or group. It can also identify non-OU containers such as the Users and Computers containers.
  • DC—Domain Component
  • Each part of the domain component is identified with separate DC identifiers. For example, a domain named pearson.itcertification.com has three domain components (pearson, itcertification, and com). These three domain components are separated like this in the DN: “DC=pearson, DC=itcertification, DC=com”. However, if the DN is listed like this “DC=pearson.itcertification.com” the command will return an error.

For example, Figure 1 shows Active Directory Users and Computers (ADUC) from a domain named pearson.itcertificatio.com. A user from the North OU (within the Sales OU) is selected.

Figure 1 User account in ADUC

Here are a few sample DNs for objects in the figure:

  • Domain: “DC=pearson, DC=itcertification, DC=com”
  • Sales OU: “OU=Sales, DC=pearson, DC=itcertification, DC=com”
  • South OU: “OU=South, OU=Sales, DC=pearson, DC=itcertification, DC=com”
  • G_NorthSalesAdmins group: “CN=G_NorthSalesAdmins, OU=South, OU=Sales, DC=pearson, DC=itcertification, DC=com”
  • DarrilGibson user account: “CN=DarrilGibson, OU=South, OU=Sales, DC=pearson, DC=itcertification, DC=com”

There are a couple of points worth emphasizing on the DNs. While I’ve used camel casing (capitalizing the first word) for readability in some of these examples, the DNs are not case sensitive. You can enter them in any mixture of upper and lower case desired. Also, if there are any spaces within the DN, you must enclose the DN in quotes. I tend to use quotes all the time even the DN doesn’t have a space to prevent accidental errors.

Look at the South OU in the figure. Notice that the South OU is farther away from the pearson.itcertification.com domain name, than the Sales OU. It also farther away from the domain name in the DN. A common mistake is to create the DN this way: “OU= Sales, OU=South, DC=pearson, DC=itcertification, DC=com”. However, the Sales OU is not within the South OU, so this will fail. The lowest level child OUs are listed first in the DN, followed by their parents.

If you can build the DN, you can easily master any of the commands in this article by substituting the DN for your environment.

Retrieve a List of Groups

The basic syntax to retrieve a list of groups is:

Dsquery group DN

For example, the following command retrieves a list of all groups in the Sales OU (and any groups in child OUs):

C:\>dsquery group "ou=sales,dc=pearson,dc=itcertification,dc=com"	
"CN=G_Sales,OU=Sales,DC=pearson,DC=itcertification,DC=com"
"CN=G_SalesAdmins,OU=Sales,DC=pearson,DC=itcertification,DC=com"
"CN=G_NorthSalesAdmins,OU=South,OU=Sales,DC=pearson,DC=itcertification,DC=com"

Notice the output includes groups in the Sales OU and a group I the South OU (a child of Sales). If you want to omit groups in the child OUs from the output, you can add the -scope onelevel switch. This command retrieves a list of the groups in the Sales OU only.

C:\>dsquery group "ou=sales,dc=pearson,dc=itcertification,dc=com" -scope onelevel
"CN=G_Sales,OU=Sales,DC=pearson,DC=itcertification,DC=com"
"CN=G_SalesAdmins,OU=Sales,DC=pearson,DC=itcertification,DC=com"

If you want to capture the output of any of these commands in a text file, you can use the redirect symbol (>). For example, this command sends the output to a file named group.txt:

C:\>dsquery group "ou=sales,dc=pearson,dc=itcertification,dc=com" > group.txt

List Group Membership for Groups and Users

You can use dsget to retrieve a list of members for a group. The basic syntax is

Dsget group DN -members

For example, this command retrieves a list of members for the Domain Admins group:

C:\>dsget group "cn=Domain Admins,cn=users,dc=pearson,dc=itcertification,dc=com" -members 

While the output of this command is valuable, you may want more. Many of the members listed will be groups, but the members of these groups aren’t included in the output. If you want to get a full listing of users and groups that are either direct or indirect members of the Domain Admins group, you can use the -expand switch like this:

C:\>dsget group "cn=Domain Admins,cn=users,dc=pearson,dc=itcertification,dc=com" -members -expand

You can also use the dsget command to identify group membership for a specific user using the memberof and expand switches. For example, this command shows the group membership for a user named Sally:

C:\>dsget user "cn=Sally,ou=sales,dc=pearson,dc=itcertification,dc=com" -memberof

Figure 2 shows the properties of Sally’s user account with the Member Of tab selected. This is the same information you’ll see from the previous command.

Figure 2 User group membership shown in ADUC

However, you can’t easily see the whole picture from ADUC. Is the G_Sales group in any other groups? Is the G_SalesAdmins group in any other groups? Sure, you could click through each of these groups to determine group membership. A simpler way is with the -expand switch, like this:

C:\>dsget user "cn=Sally,ou=sales,dc=pearson,dc=itcertification,dc=com" -memberof -expand

Retrieve a List of Users

If you want to get a list of users, you can use the dsquery user command. The basic syntax is:

Dsquery user DN -switch

For example, the following command retrieves a list of users in the domain, and redirects the output to a text file named users.txt:

C:\>dsquery user "dc=pearson,dc=itcertification,dc=com" > users.txt

If you want to retrieve a list of users within a specific OU, you can use the scope switch to limit the output like this:

C:\>dsquery user "ou=sales,dc=pearson,dc=itcertification,dc=com" -scope onelevel

Identify Inactive Accounts

The dsquery command includes an inactive switch you can use to identify inactive accounts, or accounts that haven’t been logged onto for a specific number of weeks. The inactive switch accepts an integer as a number of weeks. For example, the following commands identify users and computers that have not been logged onto in the past four weeks:

C:\>dsquery user "dc=pearson,dc=itcertification,dc=com" -inactive 4
C:\>dsquery computer "dc=pearson,dc=itcertification,dc=com" -inactive 4

While these commands are valuable on their own, you can also use pipelining to take action on the results. For example, many administrators want to disable inactive accounts. The following command shows how you can pipeline the results of the dsquery command to the dsmod command to disable the accounts:

C:\>dsquery user "dc=pearson,dc=itcertification,dc=com" -inactive 4 | dsmod user -disabled yes

Identify Disabled Accounts

If you want to list disabled user or computer accounts in your domain, you can use the disabled switch like this:

C:\>dsquery user "dc=pearson,dc=itcertification,dc=com" -disabled 
C:\>dsquery computer "dc=pearson,dc=itcertification,dc=com" -disabled

Identify Accounts with Stale Passwords

A stale password is one that hasn’t been changed in a specific number of days. While a domain password policy will force most users to change their passwords regularly (such as every 45 days), this is not enforced if the Password Never Expires setting is enabled on a user account.

It’s common to set Password Never Expires for service accounts, but the passwords for these service accounts should still be changed periodically. Unfortunately, these accounts can be forgotten and remain with the same password for the lifetime of the account.

The following command identifies user accounts with passwords that haven’t been changed in the last 60 days, and sends the output to a file named stale.txt:

C:\>dsquery user "dc=pearson,dc=itcertification,dc=com" -stalepwd  60 > stale.txt

If the password policy requires users to change their passwords every 45 days, it’s very likely that the Password Never Expires selection is enabled for these accounts.

Windows Server 2008 Portable Command Guide

The information in this article was derived from the Windows Server 2008 Portable Command Guide: MCTS 70-640, 70-642, 70-643, and MCITP 70-646, 70-647, published in May 2011. In this guide, I list many of the common commands needed to pass the listed exams and commands regularly used on the job. It isn’t a massive tome including all the commands and switches you can possibly use, but instead is a portable take-anywhere guide to show you just what you need.

  • + Share This
  • 🔖 Save To Your Account

Pearson IT Certification Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Pearson IT Certification and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Pearson IT Certification products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.pearsonitcertification.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020