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"	

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

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