How can I use the Windows Server 2003 command line to find all users in a domain who match certain criteria?

A. The Dsquery command lets you query Active Directory (AD) for most types of objects based on passed attributes. To see the full list of options, type

dsquery user /?

The basic command syntax is

dsquery user <distinguished name (DN) of root to search> <parameters to match>

For example, to find all users whose name starts with the letter "J" in domain it.uk.savilltech.com, I typed

dsquery user DC=it,DC=uk,DC=savilltech,DC=com -name J*

and the system returned

"CN=John,CN=Users,DC=it,DC=uk,DC=savilltech,DC=com"
"CN=James,CN=Users,DC=it,DC=uk,DC=savilltech,DC=com"
"CN=Jim,CN=Users,DC=it,DC=uk,DC=savilltech,DC=com"

To find all users who hadn't changed their password in more than 10 days, I typed

dsquery user DC=it,DC=uk,DC=savilltech,DC=com -stalepwd 10

To find all users who hadn't logged on using their password in the past week, I typed

dsquery user DC=it,DC=uk,DC=savilltech,DC=com -inactive 1

You can also combine switches, if necessary. For example, to find all users whose name starts with the letter "J" and who hadn't changed their passwords in more than 10 days, I typed

dsquery user DC=it,DC=uk,DC=savilltech,DC=com -name J* -stalepwd 10

Please or Register to post comments.

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.