Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active directory user count

Status
Not open for further replies.

swaughman

Technical User
Feb 27, 2004
116
GB
Urgently need a way of listing all my groups in active directory and the number of users. Don't need names just numbers.
 
How large is your organization? If it is somewhat small, you can use LDIFDE.EXE to query AD for all user accounts and then manually count the list. If you would have too many to count, you will probably want to write a script and use VBScript and ADSI.
 
Try this. I didn't write it and I can't find who did but it is quite useful.
You'll just need to change the domain name and domain controller lookup entries.

Neill

'This script finds all groups in an AD, counts all members of each group
' and writes group names and count into a file
'*** will only count users in a group, will not count nested groups ****

Const ForAppending = 8
strSoeg = "SELECT Name FROM 'LDAP://dc=domain, dc=COM' " & _
"WHERE objectCategory='group'" ' define domain to search
strDC = "dc???" ' define Domain controler to lookup groups
strPath = "c:\groups.csv" ' define file to write info

'Prepare file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(strPath, ForAppending, True)

'Prepare for AD search

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

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") = 10

objCommand.CommandText = strSoeg

Set objRecordSet = objCommand.Execute

objRecordSet.Movefirst

Do until objRecordSet.EOF
intMembers = 0
'find group name
strADsPath = objRecordSet.Fields("Name").value
' clean up group name
strGroup = rtrim(ltrim(strADsPath))

'prepare to access group strGroup
Set objGroupe = GetObject("WinNT://" & strDC & "/" & strGroup & ",group")

'count members of group strGroup
For Each objMember in objGroupe.Members
intMembers = intMembers + 1
Next
'write groupname and count to file
objTextFile.WriteLine (strGroup & ";" & intMembers )
set objGroupe = nothing
objRecordSet.MoveNext

loop

'clean up
set objRecordSet = nothing
set objGroupe = nothing
set objTextFile = nothing
set objConnection = nothing
set objCommand = nothing
set objFSO = nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top