Export all groups and members from Active Directory using VBScript code example

Example: Export all groups and members from Active Directory using VBScript

' ExportADGroupMembers.vbs
' Sample VBScript to Export Active Directory Group Members into CSV file.
' CMD Usage: CScript   
' Ex: CScript ExportADGroupMembers.vbs "Domain Admins" "C:\ADGroupMembers.csv"
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------' 
Dim groupName,strMember,csvFilePath
Dim objGroup,objMember
Dim objFSO, objCSVFile
if Wscript.arguments.count < 2 then
    Wscript.echo "Invalid input parameters"
    Wscript.echo "   "
    Wscript.echo "Script Usage:"
    Wscript.echo "-----------------------------"
    Wscript.echo "CScript   "
    Wscript.echo "   "
    Wscript.echo "Ex: CScript C:ScriptsExportADGroupMembers.vbs ""Domain Admins"" "&_
                      " ""C:\ADGroupMembers.csv"" "
    WScript.quit
else 
  ' Get the group name and csv file path from command line parameters
    groupName = WScript.Arguments(0)
    csvFilePath = WScript.Arguments(1)
end if
' Get the distinguished name of the group
Set objGroup = GetObject("LDAP://" & GetDN(groupName))
' Create CSV file 
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objCSVFile = objFSO.CreateTextFile(csvFilePath, _ 
    ForWriting, True)
' Write AD Attributes CN and distinguishedname 
' as CSV columns(first line)
 objCSVFile.Write "CN,distinguishedname"
 objCSVFile.Writeline ' New Line
' List the member’s full name in the group
For Each strMember in objGroup.Member
    Set objMember =  GetObject("LDAP://" & strMember)
   ' Retrieve values and write into CSV file.
     objCSVFile.Write objMember.CN & "," 
     objCSVFile.Write """" &strMember & """" 
     objCSVFile.Writeline  ' New Line
Next
 
Wscript.echo "AD Group '"&groupName&"' members are Exported into CSV file '"&_
             csvFilePath&"'"
WScript.quit
' Active Directory Group Members listed successfully using VBScript
 
'****************Function to Get DN of group****************
' 
Function GetDN(groupName)
Dim objRootDSE, adoCommand, adoConnection
Dim varBaseDN, varFilter, varAttributes
Dim adoRecordset
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
varDNSDomain = objRootDSE.Get("defaultNamingContext")
varBaseDN = ""
' Filter on group objects.
varFilter = "(&(objectClass=group)(|(cn="& groupName &")(name="& groupName &")))"
' Comma delimited list of attribute values to retrieve.
varAttributes = "distinguishedname"
' Construct the LDAP syntax query.
strQuery = varBaseDN & ";" & varFilter & ";" & varAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 20
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
IF(adoRecordset.EOF<>True) Then
   GetDN=adoRecordset.Fields("distinguishedname").value
Else
   'No group found 
End if
' close ado connections.
adoRecordset.Close
adoConnection.Close
End Function
'****************End of Function to Get DN of group****************

Tags:

Misc Example