Dsquery user command finds the user in the active directory that matches the specified search criteria. Active Directory user has mail attribute which stores email-address for the user.
Dsget user command-line tool displays the properties of the user in the directory. It has an
email parameter that displays the email address.
In this tutorial, we will discuss how to use the dsquery user command to get user email address and use the dsquery command to find the user by email address.
Dsquery – Get User Email Address
Using the dsquery user command with the specified distinguished name of the user, it finds the user in the active directory. It pipes the user properties to the dsget user command that uses the mail parameter to display the email address of the user.
dsquery user "CN=Chris Dore,OU=SALES,DC=SHELLPRO,DC=LOCAL" | dsget user -display -email
In the above dsquery user email address example, it gets the user email address and user display name.
C:\Windows\system32>dsquery user "CN=Chris Dore,OU=SALES,DC=SHELLPRO,DC=LOCAL" | dsget user -display -email
Chris Dore [email protected]
Use dsquery to Find User by Email Address
Use the general version of the dsquery * command to search user by email address and find the user display name and samaccountname.
In the below example, the dsquery * command uses the filter to specify the search criteria. dsquery command searches the user by the email address in the directory and gets the user object.
dsquery * -filter "mail= [email protected]" -attr displayname samaccountname
The output of the above dsquery command to get the user by email address using dsquery is:
C:\Windows\system32>dsquery * -filter "mail= [email protected]" -attr displayname samaccountname
Tom Smith toms
Dsquery Group Members Email Address
Use the dsquery group command to get the group in the active directory. It pipes the result to the dsget group command to get all the members of the group like users. It pipes the results to the dsget user command to get the group members’ email addresses.
dsquery group CN=SALESLeader,OU=SALES,DC=SHELLPRO,DC=LOCAL | dsget group -members | dsget user -display -email
The output of the above command gets the group member’s email address using the dsquery.
I hope the above article to use dsquery user to find the user email address and search the user by the email address is helpful to you.