Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get-group-members-from-ad
Const ADS_SCOPE_SUBTREE = 2
Const ForWriting = 2
'
strLDAP="OU=GroupOU,DC=contoso,DC=com"
'
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://" & strLDAP & "' WHERE objectCategory='group'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strGroupName = objRecordSet.Fields("distinguishedName").Value
GetMember(strGroupName)
objRecordSet.MoveNext
Loop
Function GetMember(strGroupName)
Set objGroup = GetObject("LDAP://" & strGroupName)
Wscript.Echo "Group Name: " & objGroup.displayName
strPath = "C:\PathToOutputFile\"
Set objFso = CreateObject("Scripting.FileSystemObject")
'Output filename is the displayname of the group
Set objOutputFile = objFSO.OpenTextFile(strPath & objGroup.displayName & ".txt", 2, True)
For Each objMember in objGroup.Members
'Entry written to file is the samAccountName
strCN=objMember.cn
objOutputFile.writeline strCN
Next
objOutputFile.Close
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment