'========================================================================== 'ExportGroupMembers.vbs 'By Timothy Bourne 'http://www.imric.co.uk 'Last Modified: 25th January 2010 '========================================================================== 'This VBscript will export the members of an Active Directory group to a 'text or CSV file. '========================================================================== Option Explicit Dim strMember Dim objGroup, objTextFile, objFSO Dim arrMemberOf Dim TextFile, i Const ForWriting = 2 i = 0 TextFile = "C:\TextFile.txt" 'Text file location (can use .txt or .csv). Set objFSO = CreateObject("Scripting.FileSystemObject") 'Check if the file already exists or not, if not then create it. Run subroutine. if objFSO.FileExists(TextFile) Then Set objTextFile = objFSO.OpenTextFile(TextFile, ForWriting) ExportGroupMembers Else Set objTextFile = objFSO.CreateTextFile(TextFile) objTextFile.Close Set objTextFile = objFSO.OpenTextFile(TextFile, ForWriting) ExportGroupMembers End If 'Subroutine connect to Active Directory and exports the group members to the text file. Sub ExportGroupMembers Set objGroup = GetObject("LDAP://CN=Example Group,OU=Example OU,DC=contoso,DC=com") objGroup.getInfo arrMemberOf = objGroup.GetEx("member") For Each strMember in arrMemberOf objTextFile.Write strMember & vbCrLf i = i + 1 Next End Sub 'Echo out how many members were exported for info. WScript.Echo "Exported the " & i & " group members to " & TextFile Wscript.Quit