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

Posted on 2007-08-03
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
    LVL 31

    Expert Comment


    Author Comment

    ok great , do i run this ?? not quite familliar with VB scripts.
    LVL 31

    Accepted Solution

    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 18

    Assisted Solution

    by:Jeremy Weisinger
    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") ("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 18

    Expert Comment

    by:Jeremy Weisinger
    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    I came across this issue when setting up a two way forest level trust. so here's the scenario: A company wildcards acquired another company, bizworks ( both Fictitious). Wild cards: windows 2003 Domain & forest functional levels - Ad domain na…
    The saying goes a bad carpenter blames his tools. In the Directory Services world a bad system administrator, well, even with the best tools they’re probably not going to become an all star.  However for the system admin who is willing to spend a li…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now