{"id":68083,"date":"2006-01-27T15:25:00","date_gmt":"2006-01-27T15:25:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/27\/how-can-i-count-the-number-of-computers-in-a-domain\/"},"modified":"2006-01-27T15:25:00","modified_gmt":"2006-01-27T15:25:00","slug":"how-can-i-count-the-number-of-computers-in-a-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-count-the-number-of-computers-in-a-domain\/","title":{"rendered":"How Can I Count the Number of Computers in a Domain?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I count the number of computers in a domain?<BR><BR>&#8212; TN<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TN. Here\u2019s 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\u2019d 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 &#8211; presumably &#8211; collated all the information to get their computer count.<\/P>\n<P>Like we said, ours might not be the most technically-savvy group here. Or anywhere, for that matter.<\/P>\n<P>Admittedly, that\u2019s a tough act to follow. And while we can\u2019t claim to have come up with anything as creative as distributing floppy disks to all your users, the following script &#8211; as humble and unimaginative as it might seem &#8211; will nonetheless give you a count of all the computers (or, at least, all the computer accounts) in your domain:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT Name FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;computer'&#8221;  \nSet objRecordSet = objCommand.Execute<\/p>\n<p>Wscript.Echo objRecordSet.RecordCount\n<\/PRE>\n<P>What\u2019s that? Why, yes, this <I>is<\/I> a script that does a search of Active Directory. And, yes, that <I>does<\/I> mean that we won\u2019t explain the details of the script today; instead we\u2019ll refer you to the two-part <I>Tales from the Script<\/I> series <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude, Where\u2019s My Printer?<\/B><\/A> if you need some background information on searching Active Directory. And, yes, that also means that &#8211; say, wait a second: who\u2019s writing this column anyway?<\/P>\n<TABLE class=\"dataTable\" id=\"EMD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. To tell you the truth, we\u2019d be happy to let you write it. But if we didn\u2019t do this column Microsoft would have to find something else for us to do. And we do <I>not<\/I> want to walk around from office-to-office collecting floppy disks!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you were probably about to point out, while this <I>is<\/I> an Active Directory search script it\u2019s 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 <B>objectCategory<\/B> equal to <I>computer<\/I>:<\/P><PRE class=\"codeSample\">objCommand.CommandText = _\n    &#8220;SELECT Name FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;computer'&#8221;\n<\/PRE>\n<P>After defining the query we call the <B>Execute<\/B> 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\u2019t 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 <B>RecordCount<\/B> that tells you how many items are in the collection. Want to know how many records are in the recordset (which &#8211; by extension &#8211; tells you how many computers are in the domain)? All you have to do is echo back the value of the RecordCount property:<\/P><PRE class=\"codeSample\">Wscript.Echo objRecordSet.RecordCount\n<\/PRE>\n<P>That\u2019s it. Like we said, it\u2019s a bit on the boring side. But it <I>does<\/I> save you a lot of money on &#8211; no, not car insurance. On <I>floppy disks<\/I>. Sheesh.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I count the number of computers in a domain?&#8212; TN Hey, TN. Here\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,19,46,3,8,5],"class_list":["post-68083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-activex-data-objects-ado","tag-computer-accounts","tag-scripting-guy","tag-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I count the number of computers in a domain?&#8212; TN Hey, TN. Here\u2019s 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}