Need script to generate list of all email accounts in Active Directory 2003

Posted on 2007-12-04
Last Modified: 2010-12-21
I need a script that will give me a .csv file of all email accounts in active directory (2003) .I don't need any info associated with the account,etc. Just need a list of all active email addresses.
Question by:FIFBA
LVL 70

Expert Comment

ID: 20407491

Author Comment

ID: 20407834
Is there a way to get the email address only into a csv file?
LVL 12

Accepted Solution

chandru_sol earned 500 total points
ID: 20408413
Try this script.........

Save the script as listemail.vbs

From the command prompt where the script resides run
cscript listemail.vbs > listemailaddress.csv

Option Explicit


Dim objRootDSE, strDNSDomain, objCommand, objConnection

Dim strBase, strFilter, strAttributes, strQuery, objRecordSet

Dim strNTName, strDN, colmail, strmail, stremail, strOU


' Determine DNS domain name.

Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")


' Use ADO to search Active Directory.

Set objCommand = CreateObject("ADODB.Command")

Set objConnection = CreateObject("ADODB.Connection")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

objCommand.ActiveConnection = objConnection


' Search entire domain.

strBase = "<LDAP://" & strDNSDomain & ">"


' Filter on contacts

strFilter = "(&(&(& (mailnickname=*)(| (&(objectCategory=person)(objectClass=user))))))"


' Comma delimited list of attribute values to retrieve.

strAttributes = "cn,distinguishedName,adspath,proxyaddresses"


' Construct the ADO query, using LDAP syntax.

strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"


' Run the query.

objCommand.CommandText = strQuery

objCommand.Properties("Page Size") = 100

objCommand.Properties("Timeout") = 30

objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute


' Enumerate the recordset and output the values retrieved in

' comma delimited format.

Do Until objRecordSet.EOF

strNTName = objRecordSet.Fields("cn").Value

strDN = objRecordSet.Fields("distinguishedName").Value

colmail = objRecordSet.Fields("proxyAddresses")


If IsEmpty(colMail) Then

' The attribute is empty (has no values).

wscript.echo "No Email address"


' The attribute has one or more values - enumerate.

For Each strMail In colMail

If Instr(strMail, "SMTP:") then

strEmail = Right(strMail, Len(strMail)-5)

End If


End If

Wscript.Echo strNTName & "," & strEmail





' Clean up.


Set objRootDSE = Nothing

Set objCommand = Nothing

Set objConnection = Nothing

Set objRecordSet = Nothing


Open in new window

LVL 12

Expert Comment

ID: 20412447
Thanks FIFBA!

Expert Comment

ID: 21799364

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Learn about cloud computing and its benefits for small business owners.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now