{"id":68913,"date":"2005-09-21T16:38:00","date_gmt":"2005-09-21T16:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/21\/how-can-i-list-all-the-groups-in-an-ou\/"},"modified":"2005-09-21T16:38:00","modified_gmt":"2005-09-21T16:38:00","slug":"how-can-i-list-all-the-groups-in-an-ou","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-groups-in-an-ou\/","title":{"rendered":"How Can I List All the Groups in an OU?"},"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 list all the groups in an OU?<BR><BR>&#8212; MK<\/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>Hey, MK. You know, sometimes people ask us a question and then immediately apologize for having asked it, believing that the question is \u201cway too easy for you guys.\u201d In turn, that seems to imply that the Scripting Guys aren\u2019t really guys at all but simply great masses of intellectual energy that sit around all day contemplating the mysteries of the cosmos. Yes, just like the old <I>Star Trek<\/I> episode where the beings from some planet had become nothing but brains, having outgrown the need for their bodies. (Some of us Scripting Guys would like to get rid of our bodies, too, but that\u2019s a different story.)<\/P>\n<P>If the truth be told, though, the Scripting Guys <I>like<\/I> easy questions. For one thing, they\u2019re easy; even great masses of intellectual energy deserve a break once in a while. More important, we know these same questions are shared by many other readers of this column. (Hey, as great masses of intellectual energy we know <I>everything<\/I>.) So don\u2019t be afraid to ask easy questions; we encourage them. And if you have hard questions, well, maybe you could track down those brain guys from <I>Star Trek<\/I>. Sure, we <I>could<\/I> answer those questions ourselves, but seeing as how <I>Star Trek<\/I> was cancelled over 35 years ago those brain guys could probably use the work. <\/P>\n<P>So how <I>do<\/I> you get a list of all the groups in an OU? Like this:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/ou=Finance,dc=fabrikam,dc=com&#8221;)\nobjOU.Filter = Array(&#8220;Group&#8221;)<\/p>\n<p>For Each objGroup in objOU\n    Wscript.Echo objGroup.Name\nNext\n<\/PRE>\n<P>We begin by binding to the Finance OU in Active Directory. This, by the way, brings up another question we get asked all the time: how do you bind to an OU that\u2019s inside another OU? For example, suppose the Finance OU was in an OU named NorthAmerica? How would you bind to the Finance OU <I>then<\/I>?<\/P>\n<P>Wow; that\u2019s <I>two<\/I> easy questions in one day. To bind to a sub-OU all you have to do is specify the complete path to that OU; in other words:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/ou=Finance,ou=NorthAmerica,dc=fabrikam,dc=com&#8221;)\n<\/PRE>\n<P>See how that works? We bind to the Finance OU, which is in the NorthAmerica OU, which is in fabrikam.com. Etc. etc.<\/P>\n<P>Having made a connection to the OU we then apply a <B>Filter<\/B>, which in this case limits the returned data to group accounts:<\/P><PRE class=\"codeSample\">objOU.Filter = Array(&#8220;Group&#8221;)\n<\/PRE>\n<P>Note that values passed to the filter must be in the form of an array. That might seem silly &#8211; an array of just one object? &#8211; but this allows us to filter on multiple object types. For example, suppose we wanted to return a list of groups and computers. In that case, we would use code similar to this:<\/P><PRE class=\"codeSample\">objOU.Filter = Array(&#8220;Group&#8221;, &#8220;Computer&#8221;)\n<\/PRE>\n<P>Finally, we set up a For Each loop and echo back the name of each item in the collection; thanks to the filter, the only things in the collection will be group accounts. <\/P>\n<P>Now, if you\u2019ll excuse us, we have to go back to figuring out why sushi &#8211; fish that hasn\u2019t been cooked &#8211; costs way more than the exact same fish that <I>has<\/I> been cooked. (And, yes, we were going to try to figure out why people would eat raw fish in the first place, but there are some things that are beyond the reach of even great masses of intellectual energy.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the groups in an OU?&#8212; MK Hey, MK. You know, sometimes people ask us a question and then immediately apologize for having asked it, believing that the question is \u201cway too easy for you guys.\u201d In turn, that seems to imply that the Scripting Guys aren\u2019t really [&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,44,43,3,5],"class_list":["post-68913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-groups","tag-ous","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the groups in an OU?&#8212; MK Hey, MK. You know, sometimes people ask us a question and then immediately apologize for having asked it, believing that the question is \u201cway too easy for you guys.\u201d In turn, that seems to imply that the Scripting Guys aren\u2019t really [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68913","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=68913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68913\/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=68913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}