HOW TO: Export all email addresses from a domain

by Bharat Suneja on September 23, 2005

Background: The Active Directory Users & Computers (ADUC) UI lets you list the mail column for each object, which displays the default (SMTP) email address for objects. You can export the list from ADUC to a CSV/txt file. However, any additional email addresses in the proxyAddresses attribute are not exported.

There’s no GUI interface (in ADUC or ESM) to list or export all email addresses. Here’s a script to do that – ListEmailAddresses.vbs.

What does it exactly do?

  • Queries Active Directory for Contacts & Groups
  • Lists their email addresses
  • Queries Users
  • Lists enabled users’ email addresses
  • Lists disabled users’ email addresses separately (was required for a certain project I did a long time back)
  • Outputs to command line and also to a text file – c:\proxyaddresses.txt
    - X.400 addresses are ignored
  • 08/07/2007: Download link updated to correct URL

{ 131 comments… read them below or add one }

1 Anonymous March 24, 2008 at 7:49 pm

Hi All,

I have an exchange question, but is unrelated to this specific topic. I was wondering if anyone here could assist or suggest a good place where I could get some assistance.

A friend of mine has a Small Business Server 2003 machine that he was using for several years for his business. He has since closed the business and set aside his machine. Now he needs to get his emails from his account off his old server.

How can he export his old emails from the database on the exchange server 2003 to something (I guess a .pst file), that he can import into his current outlook program, which seems to only have some of his latest emails, and not all of them from the previous set of years. As far as I know since the company was fairly small, there was never any archived and removed emails., They should all be there.

Thank you in advance for your consideration and support.

I can be reached at KShapiro-at-Bigfoot-dot-com. (Obviously replace the “-at-” with “@”, and “-dot-” with “.”

Sincerely,
Kevin Shapiro

Reply

2 Anonymous March 25, 2008 at 4:48 am

Thank you, very helpful :)

Reply

3 Anonymous April 2, 2008 at 2:29 pm

Dood.. You rock, you just saved me hours worth of work. Much appreciated.

Reply

4 Stewart April 7, 2008 at 3:38 am

I also get the (54, 13) (null): 0×80005000 error.

This is from running the script as is without changing any of the contents.

I know what my distinguished name is. Do I have to change the code in some way by physically inserting the distinguished name somewhere so it wull run?

Any help to get this running so I can impress my boss greatly appreciated!

Reply

5 Thomas May 13, 2008 at 5:15 am

Thanks for the script, but I’m getting the error: C:\ListEmailAddresses.vbs(52, 13) ADODB.Recordset: Item cannot be found in the collection
corresponding to the requested name or ordinal.

I’ve adjusted / entered my distinguishedName.

Any help would be appreciated!

Thanks,
Tom

Reply

6 Amidamaru May 26, 2008 at 11:15 pm

I’ve got the same message
Item cannot be found in the collection
corresponding to the requested name or ordinal.

Please help

Reply

7 snikki May 28, 2008 at 1:36 pm

What needs modified in this script to allow it to capture this data? I am new to scripting…

Reply

8 CptRoger June 4, 2008 at 1:58 am

PERFECT!

Shows the primary smtp address and all other smtp addresses (aliases) in the domain on a per user basis

Thanks a lot!

(for above, usage:
- Unzip and enter this in command prompt:

cscript C:\ListEmailAddresses\ListEmailAddresses.vbs > c:\bla.txt)

Reply

9 Anonymous June 13, 2008 at 8:47 am

This script is great, particularly usefull if you need to set up mail archiving solutions on a well established domain. Many thanks, Carlton, UK

Reply

10 Rodney August 10, 2008 at 8:04 pm

Thanks a tonne. You’ve really helped me out. :-)

Reply

11 Anonymous August 12, 2008 at 8:51 am

Hi,

Great script, but I too am getting this error:

"error: ADODB.Recordset: Item cannot be found in the collection
corresponding to the requested name or ordinal."

The path in AD I wish to output is:

nonprofit.lib.mo.us > Microsoft Exchange Security Groups > Employee Exchange group

I have modded the "distinguishedname" as follows:

strUserDN = objRecordSet.Fields("OU=Employee Exchange group,OU=Microsoft Exchange Security Groups,DC=NONPROFIT,DC=LIB,DC=MO,DC=US") 'Get User's distinguished name from Recordset into a string
set objUser= GetObject("LDAP://"& strUserDN & "")

What have I done wrong?

Thanks

Reply

12 Frost Hon August 28, 2008 at 2:27 am

Wonderful script, thank you very much. Appreciate it.

Reply

13 Anonymous August 28, 2008 at 5:17 am

Thank, works great !

Reply

14 Anonymous September 3, 2008 at 10:15 am

This script worked great for most server. I noticed that if I had an OU called the same as the domain I got ListEmailAddresses.vbs(100, 13) (null): 0×80005000 error.

Just did not get it to work on that OU.

Reply

15 Anonymous October 7, 2008 at 5:30 am

How’s it going with also listing the e-mail addresses of the Public Folders? :)

Reply

16 Bharat Suneja October 7, 2008 at 4:25 pm

@Anonymous: Haven’t had the time to pick up the Public Folders part yet…

I’m scratching my head as I think about VBScript again (given how easy this is using Exchange shell/Powershell in Exchange 2007)… :)

Reply

17 a_patch October 23, 2008 at 1:25 am

Brilliant idea, wonderful script. MS should learn from you.

Thanks

Reply

18 Anonymous November 24, 2008 at 2:27 am

Excellent tool, ran on test environment (few users) worked perfectly. But on production where I have alot more users i get the ListEmailAddresses.vbs(55, 13) (null): 0×80005000, even with the following objCommand.Properties(“Page Size”) = 5000

any advice what i need to do ?

Reply

19 Dave November 25, 2008 at 9:18 am

Awesome, thanks for this script it has saved me from a big headache, and hours of tedious work.

—Feels like Home—

Reply

20 Anonymous December 1, 2008 at 3:21 am

Great script! Works like a charm.

Thanks for sharing

Reply

21 Anonymous December 3, 2008 at 9:21 pm

To fix the (null): 0×80005000 error, you have to insert the following line in 2 places:

strUserDN = Replace(strUserDN, “/”, “\/”)

You have to add it after line 53 and the other line that says: strUserDN = objRecordSet.Fields(“distinguishedName”)

You have to do this because some domains (like mine) have global groups with the / characters in them and that will cause the query to fail.

Check out this article for the explaination:
http://www.tek-tips.com/viewthread.cfm?qid=775481

Enjoy

Reply

22 Anonymous December 12, 2008 at 12:41 pm

Thanks so much! Saved me hours.

Reply

23 Another David January 7, 2009 at 1:52 pm

I’m sorry, I’m still back with Stewart on 4/7/2008.

Are we supposed to edit the script to define our specific
“distinguishedName”? And if so, where and how?

Reply

24 Bharat Suneja January 7, 2009 at 4:40 pm

@Another David: The script picks up your AD path by querying AD for RootDSE. You generally don’t need to edit anything to run it.

See Serverless Binding and RootDSE for more info.

Reply

25 Mario_Andres January 8, 2009 at 1:24 pm

Thanks you very much… great script. I also modified it to
pull all public folders proxy address….

Reply

26 Anonymous January 21, 2009 at 10:46 pm

bravo! thx for sharing this tool, it fixed all my headache!

Reply

27 Jens February 15, 2009 at 2:02 am

This works perfectly. I wonder why there is no such function accessible via GUI in Exchange Server. Thanks a lot!

Reply

28 Anonymous February 24, 2009 at 6:34 am

thank you very much!!!!!!!!

Reply

29 Anonymous February 27, 2009 at 9:37 am

I've been using cygwin with adfind for a while now, and it's easy to come up with commands to assist with AD management.

To get all the email addresses from a domain:

adfind -default -f "(objectcategory=user)" mail | grep @ | sed -e 's/>mail: //g' | sort

You could also output the results to a file.

There probably is an easier way with adfind to do that, but this has worked for me.

Reply

30 everton_rusch April 29, 2009 at 12:04 pm

Adfind is a great tool.

But for the same funcionality of vbscript, try this:

adfind -default -f "(objectcategory=user)" proxyAddresses | grep @ | sed -e 's/>mail: //g' | sort

Reply

31 Anonymous May 7, 2009 at 2:20 am

Great script worked a treat! many thanks

Reply

32 Anonymous May 7, 2009 at 11:51 pm

Hi,

Really like the script. thanks!!!

But how do I add the user’s loginname to this???

Kind regards,

M

Reply

33 Anonymous May 13, 2009 at 6:23 am

Brilliant.

Thanks very much.

Reply

34 alouhichi May 27, 2009 at 7:26 am

Hello,

Exchange 2007 :
Is there a way to show E-mail adresses (SMTP / proxy )in all public folders, even Hidden ?
And where I can find the Folder.
No Succes with : Get-MailPublicFolder | select Name,EmailAddresses

Thanks,

Reply

35 טכנאי מחשבים June 1, 2009 at 8:02 pm

Great script thanks a lot.

Reply

36 Bob June 11, 2009 at 7:21 am

Sweet, thanks for sharing this script!

Reply

37 Michael July 2, 2009 at 10:00 am

For marketing purposes I'd like to crawl through the exchange server and get all *external* email addresses that our local users are sending / reciving from. If I had those in a CSV, I could write a vba routine to weed out the valid marketing contact. How do I do this?

Reply

38 Bharat Suneja July 11, 2009 at 1:26 pm

@Michael: This info can be found in message tracking logs.

Reply

39 Anonymous September 25, 2009 at 2:15 am

gr8 script ….. thanks much for your time and supports..keep going guys… realyy doing great help!!!!!!!!!!!!!!!!!!!!

Reply

40 תיקון מחשב September 29, 2009 at 12:20 pm

really great script! you helped me a lot.

Reply

41 Anonymous October 6, 2009 at 12:49 pm

Thought you should know, your script is still coming in very handy. Thank you. It worked beautifully.

– Campbell

Reply

42 Bharat Suneja October 6, 2009 at 12:50 pm

@Campbell: Thanks! This was one of my favorites when I wrote it a few years ago.

Reply

43 Anonymous October 12, 2009 at 12:35 pm

well, never work for me

Reply

44 Anonymous October 20, 2009 at 8:59 am

Thanks a bunch, it still works perfectly

Jan

Reply

45 Oliver Fisk November 26, 2009 at 1:32 am

Great script. Ran perfectly first time. Great output. Thanks for putting this out there.

Reply

46 Anonymous December 2, 2009 at 9:26 pm

Excellent script, thank you for your efforts!

Reply

47 traderbob.nc December 11, 2009 at 3:17 pm

Excellent – thanks for the script – I kept getting error messages too until I edited the script as the anonymous poster noted on 12-3-08 9:21P
to insert the line strUserDN=Replace(strUserDN,"/","\/") after line 53 and the other line that says strUserDN=objRecordSet.Fields ("distinguishedName")
Then your script worked like a charm.
It took me the better part of a day to figure out after reading all these posts and trying a bunch of stuff – I'm not a scripting expert so it probably took me a little longer than the average scripter
Thanks again

Reply

48 traderbob.nc December 11, 2009 at 3:27 pm

Thanks for the script. It works great. The only problem I had was the same Line 54 char 13 I see other people having… and the ADODB.Recordset error.
I inserted the string strUserDN = Replace(struserDN,"/","\/") after line 53 and after the line that says strUserDN=objRecordSet.Fields("distinguishedName")
Thanks for the script. It took me a little longer to diagnose and fix the errors than the average scripter – because I am not as experienced with scripting.
Thanks for the script.

Reply

49 Bharat Suneja December 16, 2009 at 10:25 am

@Battman: Thanks for the update!

Reply

50 Anonymous January 26, 2010 at 1:15 am

Great work. You have saved me a lot of time and effort

Reply

Leave a Comment

 

{ 1 trackback }

Previous post:

Next post: