{"id":70283,"date":"2005-03-08T13:05:00","date_gmt":"2005-03-08T13:05:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/08\/how-can-i-get-a-list-of-all-the-domains-in-a-forest\/"},"modified":"2005-03-08T13:05:00","modified_gmt":"2005-03-08T13:05:00","slug":"how-can-i-get-a-list-of-all-the-domains-in-a-forest","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-domains-in-a-forest\/","title":{"rendered":"How Can I Get a List of All the Domains in a Forest?"},"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 get a list of all the domains in a forest?<BR><BR>&#8212; NS<\/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, NS. Any time you want information about Active Directory your best bet is to go right to the source: simply ask Active Directory to provide you with this information. Here\u2019s a sample script that binds to the global catalog for the domain Fabrikam.com and searches for all the <B>domain<\/B> objects:<\/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;GC:\/\/dc=fabrikam, dc=com&#8217; WHERE objectCategory=&#8217;domain'&#8221;  \nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;Name&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>If you\u2019ve had some experience with Active Directory search scripts this particular piece of code should look very familiar; if you haven\u2019t had much (or any) experience with Active Directory search scripts we recommend you take a look at the Scripting Guys <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=25562\" target=\"_blank\">\u201cPoking Your Nose into Active Directory\u201d webcast<\/A>. (And it\u2019s worth doing, too, simply because searching Active Directory is such a powerful and useful tool. Besides, Scripting Guys webcasts are <I>always<\/I> worth watching!)<\/P>\n<P>The only thing to be careful of occurs when we bind to Active Directory. Because we want to search the global catalog we need to use the global catalog provider. Thus instead of specifying LDAP: in our binding string we specify GC:, like so:<\/P><PRE class=\"codeSample\">&#8220;SELECT Name FROM &#8216;GC:\/\/dc=fabrikam, dc=com&#8217; WHERE objectCategory=&#8217;domain'&#8221;\n<\/PRE>\n<P>Other than that, however, this is a pretty straightforward little script, and should return a list of all the domains in your forest.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of all the domains in a forest?&#8212; NS Hey, NS. Any time you want information about Active Directory your best bet is to go right to the source: simply ask Active Directory to provide you with this information. Here\u2019s a sample script that binds to the [&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,168,3,5],"class_list":["post-70283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-domains","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get a list of all the domains in a forest?&#8212; NS Hey, NS. Any time you want information about Active Directory your best bet is to go right to the source: simply ask Active Directory to provide you with this information. Here\u2019s a sample script that binds to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70283","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=70283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70283\/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=70283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}