Active directory win 2003 how do i extract the email addresses of my users ?

Posted on 2007-08-03
Medium Priority
Last Modified: 2013-11-05
We have a domain (2003) and i want to extract all the email addresses in the active directory and put it in a csv or excel file .
How can i do this ?
Question by:c_hockland
  • 2
  • 2

Author Comment

ID: 19624929
ok great , but..how do i run this ?? not quite familliar with VB scripts.
LVL 31

Accepted Solution

merowinger earned 1000 total points
ID: 19625043
type in the ip address, usernamer and password

Set oConn = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
Set oCommand = CreateObject("ADODB.Command")

oConn.Provider = "ADSDSOObject"
oConn.Properties("User ID") = "dc=nt4com,cn=Administrator"
oConn.Properties("Password") = "Password!"

oConn.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConn
oCommand.CommandText = "<LDAP://>;"& "(&(objectClass=*)(givenName=*));"& "sn,givenName,mail,distinguishedName,member,targetaddress;"& "subTree"
oCommand.Properties("Sort On") = "sn,givenName"
oCommand.Properties("Page Size") = 100
oCommand.Properties("Asynchronous") = True
Set objRecordSet = oCommand.Execute

Do Until objRecordSet.EOF
    count = count + 1
    WScript.echo "Count :" & count
    WScript.echo "SN :" & objRecordSet.Fields("sn")
    WScript.echo "givenName :" & objRecordSet.Fields("givenName")
    WScript.echo "mail :" & objRecordSet.Fields("mail")
    WScript.echo "member:" & objRecordSet.Fields("member")
    WScript.echo "distinguishedName:" & objRecordSet.Fields("distinguishedName")
    objRecordSet.MoveNext ' Next User
LVL 23

Assisted Solution

by:Jeremy Weisinger
Jeremy Weisinger earned 1000 total points
ID: 19625812
Here's a script that will write to a tab separated file (c:\UserEmailAddr.txt) all the users in AD. You can open it in Excel. It has the fields User, Email, and DN.

The script below can be run as is from any computer or account in the domain (unless there's read restrictions that have been imposed)


'* Enter the DN of the container you want to base your
'* search in, excluding the domain name (dc=domain,dc=com),
'* between the double quotes below. If you want it to
'* search the root leave it blank.

strContainer = "" 

strDirectory = "c:\"
strFile = "UserEmailAddr.txt"
strOutput = strDirectory & strFile

'------ Check or Create csv file -------

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFolder = nothing
set objFile = nothing

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" & " " & strDirectory)
Else WScript.echo "VBScript Error: " & err.number
End If

'---------- Write to file -----------

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

If strContainer = "" Then
    strADsPath = objRootDSE.Get("defaultNamingContext")
    strADsPath = strContainer & "," & objRootDSE.Get("defaultNamingContext")
End If

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

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

'--- change the DN to where you want to start your search ---
objCommand.CommandText = _
    "<LDAP://" & strADsPath & ">;" & _
        "(objectCategory=User)" & _
Set objRecordSet = objCommand.Execute
set fso = createobject("scripting.filesystemobject")

Set objFile = FSO.OpenTextFile(strOutput, 8)
objFile.WriteLine "Username" & vbTab & "Email Address" & vbTab & "Distinguished Name"

While Not objRecordSet.EOF

    Set objFile = FSO.OpenTextFile(strOutput, 8)
    objFile.WriteLine objRecordSet.Fields("Name") & vbTab & _
                      objRecordSet.Fields("Mail") & vbTab & _


LVL 23

Expert Comment

by:Jeremy Weisinger
ID: 19625829
BTW, if you're wanting to edit the script you can ignore
"'--- change the DN to where you want to start your search ---"
I left that comment there by mistake. (it's from an older version)

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

850 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