{"id":71013,"date":"2004-11-12T13:34:00","date_gmt":"2004-11-12T13:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/12\/how-can-i-tell-if-an-ou-has-any-user-accounts-in-it\/"},"modified":"2004-11-12T13:34:00","modified_gmt":"2004-11-12T13:34:00","slug":"how-can-i-tell-if-an-ou-has-any-user-accounts-in-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-an-ou-has-any-user-accounts-in-it\/","title":{"rendered":"How Can I Tell If an OU Has Any User Accounts In It?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I tell if an OU has any user accounts in it?<BR><BR>&#8212; RL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P><I>Hey, RL. As a matter of fact, there is<\/I> a way to determine whether or not an OU contains any user accounts: all you have to do is search <I>just that OU<\/I> for nothing <I>but<\/I> user accounts. When you perform a search, any \u201chits\u201d (in this case, any user accounts that are found) come back as part of a recordset. When you get the recordset back, all you have to do is check the value of the RecordCount property. If the value is 0, then there aren\u2019t any user accounts in the OU; if the value is anything other than 0, then that\u2019s how many user accounts were found. In other words, if the RecordCount is 17, then there are 17 user accounts in that OU. Child\u2019s play.<\/P>\n<P>Here\u2019s what the script looks like:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_ONELEVEL = 1<\/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_ONELEVEL <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT Name FROM &#8216;LDAP:\/\/OU=finance,dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;user'&#8221;  \nSet objRecordSet = objCommand.Execute<\/p>\n<p>Wscript.Echo \u201cNumber of user accounts: \u201c &amp;  objRecordSet.RecordCount\n<\/PRE>\n<P>We can\u2019t provide a detailed explanation of how an Active Directory search script works in this column; if you\u2019re interested in knowing more about searching Active Directory, you might want to look at a <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=25562\"><B>Webcast<\/B><\/A> we did on this topic a few months ago. However, there are three things that we <I>will<\/I> point out.<\/P>\n<P>First, note that we begin by defining a constant named <B>ADS_SCOPE_ONELEVEL<\/B> and setting it to 1. This constant tells the script to search <I>only<\/I> the target container; if there are any OUs nested within this OU, they won\u2019t get searched. If you want to search child OUs as well, then use the constant ADS_SCOPE_SUBTREE and set the value to 2.<\/P>\n<P>Second, make sure that you start the search in the target OU. Notice that our CommandText says <B>SELECT Name FROM &#8216;LDAP:\/\/OU=finance,dc=fabrikam,dc=com&#8217;<\/B>. We don\u2019t want to start in the domain root (dc=fabrikam,dc=com), we want to start in the Finance OU.<\/P>\n<P>Finally, we\u2019re searching only for user accounts; hence the clause <B>WHERE objectCategory=&#8217;user&#8217;<\/B>. What if you wanted to know if there were any computer accounts in the OU? Then just change the WHERE clause to this:<\/P><PRE class=\"codeSample\">WHERE objectCategory=&#8217;computer&#8217;\n<\/PRE>\n<P>What if you just wanted to know if <I>anything<\/I> was in the OU? (For example, perhaps you want to delete the OU, but only if it\u2019s empty.) In that case, you wouldn\u2019t use a WHERE clause at all, but would instead use this CommandText:<\/P><PRE class=\"codeSample\">SELECT Name FROM &#8216;LDAP:\/\/OU=finance,dc=fabrikam,dc=com&#8217;\n<\/PRE>\n<P>Active Directory searching is extremely powerful and extremely useful, and we\u2019d like to see people make more and better use of it. If that\u2019s the case, then shouldn\u2019t we Scripting Guys be doing something to make it easier for people to get started with Active Directory searching? Funny you should mention that; we just might have something for you in a week or two. Stay tuned!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if an OU has any user accounts in it?&#8212; RL Hey, RL. As a matter of fact, there is a way to determine whether or not an OU contains any user accounts: all you have to do is search just that OU for nothing but user accounts. When [&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,43,3,8,5],"class_list":["post-71013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-ous","tag-scripting-guy","tag-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell if an OU has any user accounts in it?&#8212; RL Hey, RL. As a matter of fact, there is a way to determine whether or not an OU contains any user accounts: all you have to do is search just that OU for nothing but user accounts. When [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71013","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=71013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71013\/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=71013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}