Using command to search AD

In Windows Server 2003 ,using Dsquery command  for getting information about objects stored in AD, including groups. This tool’s commands suite allow you to query the directory
according to specified criteria. Each of the following dsquery commands finds
objects of a specific object type, with the exception of dsquery * .

Syntax
DSQuery Computer
DSQuery Contact
DSQuery Group
DSQuery OU
DSQuery Site
DSQuery Server
DSQuery User
DSQuery Quota
DSQuery Partition
DSQuery *  (LDAP query)

Examples:
To find all computers that have been inactive for the last four weeks and
remove them from the directory:

dsquery computer -inactive 4 | dsrm

To find all users in the organizational unit
“ou=Marketing,dc=microsoft,dc=com” and add them to the Marketing Staff group:

dsquery user ou=Marketing,dc=microsoft,dc=com |    dsmod group
“cn=Marketing Staff,ou=Marketing,dc=microsoft,dc=com” -addmbr

To find all users with names starting with “John” and display his office
number:

dsquery user -name John* | dsget user -office

To display an arbitrary set of attributes of any given object in the
directory use the dsquery * command. For example, to display the
sAMAccountName, userPrincipalName and department attributes of the object
whose DN is ou=Test,dc=microsoft,dc=com:

dsquery * ou=Test,dc=microsoft,dc=com -scope base
-attr sAMAccountName userPrincipalName department

To read all attributes of the object whose DN is ou=Test,dc=microsoft,dc=com:

dsquery * ou=Test,dc=microsoft,dc=com -scope base -attr *

Examples for Dsquery computer
To find all computers in the current domain whose name starts with “ms”
and whose description starts with “desktop”, and display their DNs:

dsquery computer domainroot -name ms* -desc desktop*

To find all computers in the organizational unit (OU) given
by ou=sales,dc=micrsoft,dc=com and display their DNs:

dsquery computer ou=sales,dc=microsoft,dc=com

Examples for Dsquery group
To find all groups in the current domain whose name starts
with “ms” and whose description starts with “admin”,
and display their DNs:

dsquery group domainroot -name ms* -desc admin*

Find all groups in the domain given by dc=microsoft,dc=com
and display their DNs:

dsquery group dc=microsoft,dc=com

Examples for Dsquery OU
To find all OUs in the current domain whose name starts with “ms”
and whose description starts with “sales”, and display their DNs:

dsquery ou domainroot -name ms* -desc sales*

To find all OUs in the domain given by dc=microsoft,dc=com and display their
DNs:

dsquery ou dc=microsoft,dc=com

Examples for Dsquery user
To find all users in a given organizational unit (OU)
whose name starts with “jon” and whose account has been disabled
for logon and display their user principal names (UPNs):

dsquery user ou=Test,dc=microsoft,dc=com -o upn -name jon* -disabled

To find all users in only the current domain, whose names end with “smith”
and who have been inactive for 3 weeks or more, and display their DNs:

dsquery user domainroot -name *smith -inactive 3

To find all users in the OU given by ou=sales,dc=microsoft,dc=com and display
their UPNs:

dsquery user ou=sales,dc=microsoft,dc=com -o upn

Example to display the distinguished names of all users in only the current domain
whose names end with “Smith” and who have been inactive for 120 days (17+
weeks rounded to 18 weeks) or more, type:

dsquery user domainroot -name *smith -inactive 18

Read more http://technet.microsoft.com/en-us/library/cc732952%28WS.10%29.aspx

Advertisements

Posted on January 22, 2012, in Uncategorized, Windows. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: