Posted by: Andy Grogan | August 22, 2007

How to export all e-mail addresses in AD to a csv file..

I originally blogged about this a year of so ago, but when I moved over from Blogging providers I left the original article behind – Doh!.

Below is a sample script that when you run it against your domain controller (or you your Exchange server) you will be prompted for a CSV file which the script will then fill with all the e-mail addresses (including proxybased) in Active Directory.

I have placed a copy of the script for download here – hope you find it useful.

Dim strFilePath
strFilePath = Inputbox(“Please provide a path for the CSV file”,”Provide Filename”,”*.csv”)
Dim rootDSE, domainObject
Set rootDSE=GetObject(“LDAP://RootDSE“)
domainContainer = rootDSE.Get(“defaultNamingContext”)
Set domainObject = GetObject(“LDAP://” & domainContainer)
Set fs = CreateObject (“Scripting.FileSystemObject”)
Set userFile = fs.CreateTextFile (strFilePath)

ExportUsers(domainObject)

Set oDomain = Nothing
MsgBox “Script Completed”
WScript.Quit

Sub ExportUsers(oObject)
 Dim oUser
 For Each oUser in oObject
  Select Case oUser.Class
   Case “user”
    If oUser.mail <> “” then
    
     userFile.Write “,” & oUser.displayName &  “,” & oUser.sAMAccountName & “,” & oUser.userprincipalname & “,”
    On error Resume Next
     for each email in oUser.proxyAddresses
      userFile.Write email & “,”
     next
      userFile.WriteLine “”
    End if

   Case “organizationalUnit” , “container”
    If UsersinOU (oUser) then ExportUsers(oUser) End if
   
   End select
 Next

End Sub

Function UsersinOU (oObject)
 Dim oUser
 UsersinOU = False
  for Each oUser in oObject
   Select Case oUser.Class
    Case “organizationalUnit” , “container”
     UsersinOU = UsersinOU(oUser)
     Case “user”
     UsersinOU = True
   End select
  Next
End Function


Responses

  1. where do you enter the domain at?


Leave a response

Your response:

Categories