User Count

Dear Team,

We are looking for a mechanism to count the number of users in a OU and all the Sub-OU's. We have a script which counts the number of users in the root OU only and does not go beyond that. We have a OU structure which is 3-4 level down and want to know the count of user objects in all those OU's.
Neo_78Asked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

> but I want Sub-OU wise breakup wherein it should be showing the number of users present in each subOU

Oh I see. Okay then. Like this?

Chris


Function GetUserCount(objOU)
  objOU.Filter = Array("user")
  For Each objUser in objOU
    If InStr(1, objUser.Get("objectCategory"), "person", vbTextCompare) > 0 Then
      i = i + 1
    End If
  Next
  If i > 0 Then
    objOU.GetInfoEx Array("canonicalName"), 0
    WScript.Echo objOU.Get("canonicalName") & ": " & i
  End If

  objOU.Filter = Array("organizationalUnit")
  For Each objSubOU in objOU
    GetUserCount(objSubOU)
  Next
End Function

Set objOU = GetObject("LDAP://OU=Somewhere,DC=domain,DC=com")
GetUserCount objOU

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

In VbScript, as follows.

And in PowerShell, because it's absurdly simple provided you have it and the Quest CmdLets:

PowerShell : http://support.microsoft.com/kb/968929
Quest CmdLets : http://www.quest.com/powershell/activeroles-server.aspx

(Get-QADUser -SearchRoot "OU=somewhere,DC=domain,DC=com" | Measure-Object).Count

Chris
Dim strLdapPath : strLdapPath = "LDAP://OU=Somewhere,DC=domain,DC=com"

Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Dim objCommand : Set objCommand = Createobject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000

objCommand.CommandText = "<" & strLdapPath & ">;" & _
  "(&(objectClass=user)(objectCategory=person));name,distinguishedName;subtree"

Dim objRecordSet : Set objRecordSet = objCommand.Execute

WScript.Echo "User Count: " & objRecordSet.RecordCount

Open in new window

0
 
aamodtCommented:
Windows 2008 server:
dsquery user | dsget user -display

Getting all users and  displays the -display.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
aamodtCommented:
sorry,  you can properly  integrate this with a count script/function, im going to look abit more into this :)
0
 
Neo_78Author Commented:
Dear ,

Thanks for the reply, Thescript which you hae provided gives the total number of users in a OU but I want Sub-OU wise breakup wherein it should be showing the number of users present in each subOU. I have got one script which is mentioned below. It Only shows the number of users in top OU. I want to run this script in loop so that it should show me the counts of all the subOU's also.

Please help
=================================================================================
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set oFile = FileSystem.CreateTextFile("CountUsers.csv", True)

OUinfo = InputBox("OU NAme")
Set OU = GetObject("LDAP://Domain.Com/OU=" & OUinfo & ",OU=One,DC=Domain,DC=Com")
OU.Filter = Array("organizationalUnit")

For Each oUnit In OU
oUnit.Filter = Array("user")
i = 0
For Each oUser In oUnit
i = i + 1
Next
Ounit.getinfo
proplist = array( "canonicalName")
Ounit.getinfoex proplist, 0

oFile.WriteLine (Ounit.canonicalName & "," & i )
'WScript.echo Ounit.canonicalName & " contains " & i & " users."
Next
Wscript.echo "Done, results have benn written to CountUsers.csv ..."
================================================================================
0
 
Chris DentPowerShell DeveloperCommented:

Just in case, with a few more comments and writes output to a file.

Chris
Option Explicit

Function GetUserCount(objOU)
  ' Apply a filter for users and count
  objOU.Filter = Array("user")
  Dim objUser
  Dim i : i = 0
  For Each objUser in objOU
    ' Must check category as well, computers also have user in the objectClass
    If InStr(1, objUser.Get("objectCategory"), "person", vbTextCompare) > 0 Then
      i = i + 1
    End If
  Next

  ' Ignore OUs with a zero user count
  If i > 0 Then
    objOU.GetInfoEx Array("canonicalName"), 0
    ' Write the canonical name and user count to the file
    objFile.WriteLine objOU.Get("canonicalName") & "," & i
  End If

  ' Apply a filter for organizationalUnit and recurse down the structure
  objOU.Filter = Array("organizationalUnit")
  Dim objSubOU
  For Each objSubOU in objOU
    GetUserCount(objSubOU)
  Next
End Function

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile("CountUsers.csv", 2, True, 0)

Dim OUinfo : OUinfo = InputBox("OU Name")
Dim objOU : Set objOU = GetObject("LDAP://Domain.Com/OU=" & OUinfo & ",OU=One,DC=Domain,DC=Com")

GetUserCount objOU

Wscript.echo "Done, results have benn written to CountUsers.csv ..."

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.