How Can I Count the Number of Computers in a Domain?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I count the number of computers in a domain?

— TN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TN. Here’s a true story for you. About 5 years ago, when one of the Scripting Guys first started at Microsoft, his group here (which might not be the most technically-savvy group at the company) needed to get a count of all our computers. How did they do that? Why, exactly the way you’d think they would: they distributed floppy disks to everyone. On that floppy disk was a batch file that would grab the computer name and write it to the disk. Everyone was instructed to place the disk in the floppy drive of each of their machines and then run the batch file. Later that week, IT types came around, collected all the disks, and – presumably – collated all the information to get their computer count.

Like we said, ours might not be the most technically-savvy group here. Or anywhere, for that matter.

Admittedly, that’s a tough act to follow. And while we can’t claim to have come up with anything as creative as distributing floppy disks to all your users, the following script – as humble and unimaginative as it might seem – will nonetheless give you a count of all the computers (or, at least, all the computer accounts) in your domain:

On Error Resume Next


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 Name FROM ‘LDAP://dc=fabrikam,dc=com’ WHERE objectCategory=’computer'”
Set objRecordSet = objCommand.Execute

Wscript.Echo objRecordSet.RecordCount

What’s that? Why, yes, this is a script that does a search of Active Directory. And, yes, that does mean that we won’t explain the details of the script today; instead we’ll refer you to the two-part Tales from the Script series Dude, Where’s My Printer? if you need some background information on searching Active Directory. And, yes, that also means that – say, wait a second: who’s writing this column anyway?

Note. To tell you the truth, we’d be happy to let you write it. But if we didn’t do this column Microsoft would have to find something else for us to do. And we do not want to walk around from office-to-office collecting floppy disks!

As you were probably about to point out, while this is an Active Directory search script it’s actually a bit simpler than most such scripts. For example, our SQL command is pretty bare-bones: we simply write a query that returns a collection of all the objects that have an objectCategory equal to computer:

objCommand.CommandText = _
“SELECT Name FROM ‘LDAP://dc=fabrikam,dc=com’ WHERE objectCategory=’computer'”

After defining the query we call the Execute method, which returns a recordset consisting of all the computer accounts found in the domain. At this point we would normally set up a Do Loop to walk through all the items in the collection (that is, all the computers in the domain) and echo back whatever properties we specified in our query. For this script, however, we don’t have to bother with any of that. Why not? Well, all we wanted was a count of the number of computers. As it turns out, ADO (ActiveX Data Objects) recordsets all have a property named RecordCount that tells you how many items are in the collection. Want to know how many records are in the recordset (which – by extension – tells you how many computers are in the domain)? All you have to do is echo back the value of the RecordCount property:

Wscript.Echo objRecordSet.RecordCount

That’s it. Like we said, it’s a bit on the boring side. But it does save you a lot of money on – no, not car insurance. On floppy disks. Sheesh.

Comments (6)

  1. jrv says:


    Set objCommand =   CreateObject("ADODB.Command")

    objConnection.Provider = "ADsDSOObject"

    objConnection.Open "Active Directory Provider"

    ' filter for only computer accounts that are enabled

    strFilter = "(&(objectClass=computer)(userAccountControl:1.2.840.113556.1.4.804:=2))"

    strAttributes = "name,distinguishedName"

    strQuery = strDomainDN & ";" & strFilter & ";" & strAttributes & ";subtree"

    Set objCommand.ActiveConnection = objConnection

    objCommand.CommandText = strQuery

    objCommand.Properties("Page Size") = 9999

    Set objRecordSet = objCommand.Execute

  2. Anonymous says:

    Great script, but my challenge is to get hold of the number of computers in each OU, not included the sub-OUs. How can I do that?



  3. bill says:

    Where does it output the information to? I am running the script but nothing seems to happen should it be piped out to a file?

  4. Andy says:


    On the line:

    objCommand.CommandText = _

       "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE objectCategory='computer'"  

    Change the LDAP connection to match your domain.

    After you change that and run script, it pops a box up with the number of computers.

  5. Dave says:

    How would you modify to show only active computers (not disabled).