List of email addresses including all aliases in Active Directory

Posted on 2009-02-19
Last Modified: 2012-05-06
Hey there!

I have found a nice vb script that will extract the email addresses for my active directory users.

However I need to extract ALL addresses, including aliases.

I'm trying to clean up my spam filter system that was not properly configured, and I need to delete about 500 users from it, and it would be handy to have a all-inclusive list of all email addresses and aliases to work from.  This is going to take a long time as it is, and without a list to work from it will take too long.

Attached is the script that will produce a list of the main email addresses for the users, and it works great, but it is not exactly what I need.  This script is already posted, but I am including it here for the people that find this post in a search engine, who's needs are met by this script.


'* 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:\work"

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 & _







Open in new window

Question by:georgew3
    LVL 57

    Accepted Solution

    one of the VBscript guys will help with that but can I suggest another method.  That is using Joe Richards adfind tool
    adfind -b DC=yourdomain,DC=com(or whatever your suffix is)  -f  "&(objectcategory=person)(objectclass=user)"  samaccountname mail mailnickname proxyaddresses -csv >  c:\usersemail.csv
    That will spit out those attributes into a csv file for you.
    mailnickname is the alias.
    Please let me know how that works for you.
    LVL 6

    Assisted Solution

    This script loops through all the proxy email addresses.  Hope it helps.
    LVL 8

    Assisted Solution

    LVL 70

    Assisted Solution

    by:Chris Dent


    Can you export a list of addresses from the spam filter? It would be easy enough to verify those with what you have in Active Directory, would potentially save a lot of time (rather than comparing static lists).

    LVL 18

    Assisted Solution

    by:Jeremy Weisinger
    Hi George,

    I like Mike's suggestion of using adfind.

    I would use this string instead as it will filter out the x400 addresses and it will also get any mail enabled object:
    adfind.exe -sc exchaddresses:smtp -csv > allemail.csv

    Mike, I think he means "proxy addresses" when he says aliases

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Some time ago I faced the need to use a uniform folder structure that spanned across numerous sites of an enterprise to be used as a common repository for the Software packages of the Configuration Manager 2007 infrastructure. Because the procedu…
    Installing a printer using group policy preferences is not that hard let’s take a look at it. First lets open up your group policy console and edit the policy you want to add it to. I recommend creating a new policy for each printer makes it a l…
    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 configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    731 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

    16 Experts available now in Live!

    Get 1:1 Help Now