{"id":69513,"date":"2005-06-27T11:52:00","date_gmt":"2005-06-27T11:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/27\/how-can-i-retrieve-a-list-of-ous-that-have-a-specific-word-in-their-name\/"},"modified":"2005-06-27T11:52:00","modified_gmt":"2005-06-27T11:52:00","slug":"how-can-i-retrieve-a-list-of-ous-that-have-a-specific-word-in-their-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-retrieve-a-list-of-ous-that-have-a-specific-word-in-their-name\/","title":{"rendered":"How Can I Retrieve a List of OUs that have a Specific Word in Their Name?"},"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! We have a number of OUs in our domain that have the word <I>Servers<\/I> in the OU name. How can I retrieve a list of those OUs?<BR><BR>&#8212; AM<\/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, AM. One of the Scripting Guys has a 15-year-old son who, whenever he hears his father tell a story about the good old days, rolls his eyes and says, \u201cYou\u2019re just one of those old guys still living in the past.\u201d (Of course, in the same breath this Scripting Son will say, \u201cRemember when I was 10 and hit that home run against the Rockies?\u201d but somehow that\u2019s different.)<\/P>\n<P>We mention this because your question takes us back to the good days, back to the glory years of the <I>Hey, Scripting Guy!<\/I> column. Back then it seemed like every other question we took had the same answer: you can find what you\u2019re looking for by searching Active Directory. Did we do that because searching Active Directory is an incredibly powerful tool for system administrators, or did we do that because we were lazy and just liked giving the same answer over and over again? Well, we <I>are<\/I> lazy, but the truth is, ADO searches of Active Directory are enormously useful to system administrators. If you need a list of user accounts or a list of groups or a list of computers or a list of phone numbers or a list of just about anything, there\u2019s a good chance you\u2019ll find that information stored in Active Directory.<\/P>\n<P>So, roll your eyes and sigh if you must, but here\u2019s the answer to your question: you can find what you\u2019re looking for by searching Active Directory. In fact, you can retrieve a list of OUs that have the word \u201cServers\u201d in their name by using a script similar to this:<\/P><PRE class=\"codeSample\">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 ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; &#8221; &amp; _\n        &#8220;WHERE objectCategory=&#8217;organizationalUnit&#8217; AND Name = &#8216;*Servers*'&#8221;  <\/p>\n<p>Set objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;ADsPath&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>We won\u2019t go over this script in detail today; if you don\u2019t have much experience with searching Active Directory you might take a look at our two-part series <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude, Where\u2019s My Printer?<\/B><\/A>, a series that explains the <B>ADODB.Connection<\/B> object and the <B>Searchscope<\/B> and all the other crazy-looking code found in this script. Instead we\u2019ll just focus on the SQL query that actually returns the list of OUs for us:<\/P><PRE class=\"codeSample\">objCommand.CommandText = _\n    &#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; &#8221; &amp; _\n        &#8220;WHERE objectCategory=&#8217;organizationalUnit&#8217; AND Name = &#8216;*Servers*'&#8221;\n<\/PRE>\n<P>What we\u2019re doing here is searching the fabrikam.com domain and retrieving the ADsPath attribute. (Obviously we could have picked any other attribute, or even multiple attributes. We chose ADsPath because once you know the ADsPath for an object you can easily bind to that object and retrieve values for all its attributes.) That part\u2019s pretty straightforward; what we\u2019re <I>really<\/I> interested in is the WHERE clause:<\/P><PRE class=\"codeSample\">WHERE objectCategory=&#8217;organizationalUnit&#8217; AND Name = &#8216;*Servers*&#8217;\n<\/PRE>\n<P>We use the WHERE clause to specify the criteria for our search, and in this case we\u2019re looking for two things:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>An <B>objectCategory<\/B> equal to <B>organizationalUnit<\/B>. As you might have guessed, an object with an objectCategory equal to organizationalUnit will be an OU. This part of our query weeds out users, computers, groups, sites, subnets, and any other kind of object stored in Active Directory.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>OUs that have the word <I>Servers<\/I> somewhere in their <B>Name<\/B> attribute. Note the use of the wildcard character (the asterisk). This syntax *Servers* basically says, \u201cWe\u2019re looking for the word Servers anywhere in the Name attribute. We don\u2019t care what, if anything, comes before the word Servers (the first asterisk), and we don\u2019t care what, if anything, comes after the word Servers (the second asterisk). We want any OU that has the word Servers somewhere in its Name.\u201d<\/P>\n<P>We could, of course, modify this syntax. What if we wanted to search only for Names that <I>started<\/I> with Servers (Servers-1, Servers-2, etc.)? Then we could search for this: Servers*. As you can see, in this case our Name has to start with the word Servers; we don\u2019t care what comes after that. If we wanted to search for Names that <I>ended<\/I> with the word Servers (Asian Servers, North America Servers, etc.) we\u2019d use this syntax: *Servers. It\u2019s really no different than using wildcard characters to search for files or folders, something you\u2019ve probably done a million times.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>After defining the query we call the <B>Execute<\/B> method; this actually runs the query and returns a recordset containing all the OUs that have the word Servers somewhere in the Name. We then loop through the recordset and echo the ADsPath for each of these OUs. That\u2019s all there is to it.<\/P>\n<P>Incidentally, it\u2019s usually not recommended that you search for text in the middle of a string (*Servers*); that\u2019s because a search like that can be resource-intensive and take awhile for the domain controller to process. We can get away with it here because we\u2019re assuming you don\u2019t have millions of OUs to check; typically domains have far fewer OUs than they do, say, users or groups. Therefore we don\u2019t expect this search to cause any problems.<\/P>\n<P>Does that answer your question? Good. And, although you didn\u2019t ask, we\u2019re sure you\u2019d like to hear about the time we almost-single-handedly beat Hanford High School, at that time undefeated and the number 1 ranked baseball team in the state of &#8211; oh, OK. But if the Scripting Son wants to talk about his home run from this season, you better not listen to him, either. (Although that <I>was<\/I> a nice hit. It reminded Scripting Dad of that one day in Richland when he\u2026.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have a number of OUs in our domain that have the word Servers in the OU name. How can I retrieve a list of those OUs?&#8212; AM Hey, AM. One of the Scripting Guys has a 15-year-old son who, whenever he hears his father tell a story about the good old [&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-69513","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! We have a number of OUs in our domain that have the word Servers in the OU name. How can I retrieve a list of those OUs?&#8212; AM Hey, AM. One of the Scripting Guys has a 15-year-old son who, whenever he hears his father tell a story about the good old [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69513","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=69513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69513\/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=69513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}