Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On Windows XP in an Active directory environment - what is the easiest way for me to query a user's email address from AD given their username on the command line.

(Assuming I know where it is kept normally in tree).

(I know about net user loginname /domain but I just want the email address element back.)

share|improve this question
Please note that if you are in a complex Exchange the "mail" attribute may not be the email address you're looking for. You will also need to look at the multi-valued attribute "proxyAddresses" on the user object. – Ryan Fisher Jun 17 '09 at 14:15

10 Answers 10

up vote 15 down vote accepted
dsquery user -name "user name"|dsget user -samid -email -display
share|improve this answer
Missed it by that much... :) – TheCleaner Jun 17 '09 at 13:23
Ok - perhaps I wasn't clear - but you're close enough - what I guess I wanted was: dsquery user -samid "loginname" |dsget user -email – Hawkeye Jun 18 '09 at 1:09

dsquery user -name "Firstname Lastname" | dsget user -email

share|improve this answer

something like this dsquery might work.

query email by username dsquery.exe * -filter "(&(objectClass=user)(!(objectClass=computer)(sAMAccountName=username)))" | dsget user -email

I misread the post first and thought you wanted user name from email name. That's why i posted this one. dsquery.exe * -filter "(&(objectClass=user)(!(objectClass=computer)(" -attr username

based on some scripts at work and this site which has some other ideas about using csvde.exe

share|improve this answer

adfind -sc u:"username" mail

share|improve this answer
Thats a realyl useful tool, although it would be more awesome if the person supplied the source code. – Justin Dearing May 4 '11 at 20:53

If the email that you want is also the User Principal Name, you can get it with

whoami /upn

However, this only works to get the email of the current user, not any user as the question originally postulated.

share|improve this answer
Used this method by running cmd as the target user. Worked like a charm – Daniel K Jan 12 at 19:26

Install Powershell, and the QuestAD addon pack. Then it is something like:

(get-qaduser 'bobsusername').emailAddress
share|improve this answer

You can write simple VBScript to query thru LDAP Create a file with VBS extension

Put inside something like this

On Error Resume Next
Set objUser = GetObject _


strMail = objUser.Get("mail")

WScript.echo "mail: " & strMail

Put correct USER NAME into LDAP query string, run VBS file and enjoy :)

If it is your first time you work with LDAP, it could be a little bit complicated to write LDAP query In order to recognize the LDAP path to the user (i.e. what you need to put after LDAP://) you may download Active Directory Explorer from Microsoft Run explorer, navigate to the user and see what it shows in the Path text box

In my case it was something like CN=[user name],CN=Users,DC=[city_name],DC=[company_name],DC=com,

share|improve this answer

Found this thread that helped me get what I wanted. To get any AD users attributes into environment variables. This script takes all wanted attributes from logged in user and sets a corresponding environment variable. I prefixed the variables but that is optional, so variable name becomes "AD[attribute name]". Attributes is of your choice, just add or remove your attribute after -attr. Not very useful for multivalue-attributes though. Last (one) value goes into the environment variable.

This script is local to current cmd.exe

for /F "tokens=1,* delims=: " %%A in ('dsquery * domainroot -l -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=%USERNAME%))" -attr adminDescription employeetype company department physicalDeliveryOfficeName street title mail') do set AD%%A=%%B

To get global environment variables in windows we can use "setx" in windows 7. (For loginscript perhaps... but much slower.)

for /F "tokens=1,* delims=: " %%A in ('dsquery * domainroot -l -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=%USERNAME%))" -attr adminDescription employeetype company department physicalDeliveryOfficeName street title mail') do set AD%%A=%%B& setx AD%%A "%%~B" > NUL

:EDIT: a space character at end of set-statement in example 2 caused value to end with empty space. Removed it to correct. ( Set %%A=%%B& setx... ) Also found out that you must export at least two attributes for script to work properly.

A late responce, but if it can help anyone out there I'm happy.

share|improve this answer

LINQ to everything! For convenience:

1) In query properties of LinqPad, add a reference to System.DirectoryServices.AccountManagement.dll. 2) Additional Namespace Import: System.DirectoryServices.AccountManagement

using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MyDomain))
  using(UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "MyUserID"))
share|improve this answer

I don't know it's matched to the thread starter mean or not. But I just find a solution of my existing problem that was already solved after browse this thread. Finding USER LOGIN ID based on KNOWN MAIL ADDRESS. :)

C:\Users\MrCMD>for /f "delims=" %u in ('type salesforce-uid-mail-address.txt') do @dsquery.exe * -filter "(&(objectClass=user)(!(objectClass=computer)(mail=%u)))">>"salesforce-uid-cn.txt"
│ Executed Wed 07/10/2013  8:29:55.05 │ As [MrCMD]
C:\Users\MrCMD>for /f "delims=" %u in ('type salesforce-uid-cn.txt') do @dsget.exe user %u -samid -l|find "samid" /i>>"salesforce-uid-samid.txt"
│ Executed Wed 07/10/2013  8:31:56.40 │ As [MrCMD]

File [salesforce-uid-mail-address.txt] contains list of e-mail addresses. File [salesforce-uid-cn.txt] contains "complete CN with path". And file [salesforce-uid-samid.txt] contains "found SAMID" alias "user login name". That's all folks. Any ideas for improvement are welcome. :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.