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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
aamodtCommented:
sorry,  you can properly  integrate this with a count script/function, im going to look abit more into this :)
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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:

> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.