Home > Articles > Microsoft > MCTS

Retrieving Information from Active Directory with Dsquery and Dsget

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.

vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |