get emailaddress of user in Active directory using ldap query

Currently I have LDAP query

(&(objectCategory=contact)(objectCategory=person)(mail=*)(objectClass=user)) which is returning the user name .I want only thr email address of the user in return
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:

LDAP queries define which objects should be returned, they don't define which properties should be returned and displayed.

How are you using the filter above at the moment? That's where we need to define what you see.

sourabh1Author Commented:
In my case i need to pass the username  and in return i need its email address
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Chris DentPowerShell DeveloperCommented:

What are you using to do this?

I woud suggest using powershell for this you can download the quest AD cmdlets using the link below and they work on 2003 as well as 2008 DCs. You shoud install them on your machine rather than the DC. 

you would use the command below to find this for just one user
get-qaduser Samaccountname | select samaccountname,mail

or is you want all enabled users accountname and email use this:

get-qaduser -ldapfilter "(mail=*)" -enabled | select samaccountname,mail | export-csv c:\temp\userlist.csv

Farhan KaziSystems EngineerCommented:
If you are apply this query using ADUC snap-in -> Saved Queries then make sure you have selected "E-Mail Address" from add / remove columns.
Right click on Query that have you made -> View -> Add/Remove Columns -> E-Mail Address

Alternatively you can perform this from command line:
Click Start -> Run -> Cmd.exe -> OK

DSQuery * -Filter "(&(objectCategory=contact)(objectCategory=person)(mail=*) (objectClass=user))" -Attr samAccountName Mail -Limit 0

Hope this helps!

Dear Friend,
You can follow any one of them. Please check and reply.

1, dsquery user -name "user name"|dsget user -samid -email -display

2, Ok - perhaps I wasn't clear - but you're close enough - what I guess I wanted was: dsquery user -samid "loginname" |dsget user -email
sourabh1Author Commented:
Yes Porpatahm u r right
sourabh1Author Commented:
I want  that LDAP query which will take loginname and return me the emailid.
No dotnet code for this
Shreedhar EtteCommented:

In your query put mail=*$EmailAddress*

and check.

I hope this helps,
Dear Friends,

              Please mention did the Comment Helpful?

Then only i will know the progress..................

Try this.. It will return the username and email address. If the email address isn't there it will only return the username.

'on error resume next
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 30000

objCommand.CommandText = _
 "<LDAP://dc=company,dc=com>;" & _
 "(&(objectCategory=person)(objectClass=user));" & _
 "cn, mail;subtree"

Set objRecordSet = objCommand.Execute
'wscript.echo objRecordset.RecordCount
If objRecordSet.RecordCount > 0 Then

 While Not objRecordset.EOF
strUser = objRecordset.Fields("cn")
strMail = objRecordset.Fields("mail")
if strMail <> "" then
wscript.echo strUser & "," & strMail
wscript.echo strUser & ","
End if


End If

The script I provided above will provide the info needed.
Chris DentPowerShell DeveloperCommented:

I recommend Split between these:

http:#29771789 (shreedhar)
http:#29774205 (pubeheed)
http:#29777319 (farhankazi)
http:#29782221 (Porpatham)
http:#30030433 (mrfixit584)

All of the methods provided are viable.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.