Submitted By: Laird Bishop

Lists all the members of an Active Directory group.

Visual Basic
Edit|Remove
' COMMENT: Returns a listing of user account which are members of a group 
'
'==========================================================================
Option Explicit

Const OVERWRITE_EXISTING = True

Dim objGroup, colNamedArguments, strGroup, arrMembers, objUser, 
Dim strUser, objFSO, objOutputFile, strOutputFileName, strLDAPPath
Dim colUnnamedArguments, UnnamedArgument, strHelp, WshShell
Set WshShell = CreateObject("Wscript.Shell")
Set colNamedArguments = WScript.Arguments.Named
Set colUnnamedArguments = WScript.Arguments
Set objFSO = CreateObject("Scripting.FilesystemObject")

For Each UnnamedArgument In colUnnamedArguments
	If UnnamedArgument = "/?" Then 
		DisplayHelp
		WScript.Quit
	End If
	If lcase(UnnamedArgument) = "help" Then 
		DisplayHelp
		WScript.Quit
	End If
		If lcase(UnnamedArgument) = "/help" Then 
		DisplayHelp
		WScript.Quit
	End If
Next


If colNamedArguments.Exists("G") Then
	strGroup = colNamedArguments.Item("G")
Else WScript.Echo "Argument /G:<Group Name> is required"
	DisplayHelp
	WScript.Quit
End If
If colNamedArguments.Exists("Out") Then
	If objFSO.FolderExists(colNamedArguments.Item("Out")) = false Then
		objFSO.CreateFolder(colNamedArguments.Item("Out"))
	End If
	strOutputFileName = colNamedArguments.Item("Out")& "\" & strGroup & "_Members.csv"
Else
	strOutputFileName = ".\" & strGroup & "_Members.csv"
End If
If colNamedArguments.Exists("LDAP") Then
	strLDAPPath = "," & colNamedArguments.Item("LDAP")
Else
'   Replace this string with the default LDAP path for your company
	strLDAPPath = ",ou=Groups,ou=corp,dc=corp,dc=ds,dc=yourcompany,dc=com"
End If

Set objGroup = GetObject("LDAP://cn=" & strGroup & strLDAPPath)
objGroup.GetInfo

Set objOutputFile = objFSO.CreateTextFile(strOutputFileName,OVERWRITE_EXISTING)
objOutputFile.WriteLine("User Name" & "," & "Last Name" & "," & "First Name" & _
    "," & "Department")

arrMembers = objGroup.GetEx("member")

For Each strUser in arrMembers
	Set objUser = GetObject("LDAP://" & strUser)
    objOutputFile.WriteLine(objUser.SAMAccountName& "," & objUser.sn & "," & _
        objUser.GivenName & "," & objUser.Department)
Next

objOutputFile.Close

WshShell.Run strOutputFileName,1,False

Sub DisplayHelp ()
    strHelp =   "Usage: wscript UGMembers.vbs /G:<Group Name> [/Out:<Output file path>]" & _
        " [/LDAP:<Full LDAP Path>]" & vbCrLf & vbCrLf & _
        "<Group Name> is required" & vbCrLf & vbCrLf & _
        "Output file will be named <Group Name>_Members.csv"  & vbCrLf & vbCrLf & _
        "<Output file path> defaults to the same directory from" & vbcrlf & _
        "which the script was run if this argument is omitted." & vbCrLf & vbCrLf & _
        "<Full LDAP Path> is the fully distinguised path under which the group resides." & vbCrLf & _
        "If this argument is omitted, the LDAP path defaults to " & vbCrLf & _
        "ou=Groups,ou=corp,dc=corp,dc=ds,dc=Yourcompany,dc=com"
    WScript.Echo strHelp
End Sub