{"id":68463,"date":"2005-11-23T15:00:00","date_gmt":"2005-11-23T15:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/23\/how-can-i-get-a-list-of-all-the-groups-whose-name-starts-with-g-s-group\/"},"modified":"2005-11-23T15:00:00","modified_gmt":"2005-11-23T15:00:00","slug":"how-can-i-get-a-list-of-all-the-groups-whose-name-starts-with-g-s-group","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-all-the-groups-whose-name-starts-with-g-s-group\/","title":{"rendered":"How Can I Get a List of All the Groups Whose Name Starts with G-S-Group?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! How can I use wildcards to get a list of all the groups whose name starts with G-S-Group?<\/p>\n<p>&#8212; JD<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, JD. It&rsquo;s the day before Thanksgiving, and one of the Scripting Guys is headed out of town as soon as he finishes his <i>Hey, Scripting Guy!<\/i> column. And what a coincidence: he happened to pick a question about searching Active Directory, the kind of column he could write in his sleep (and probably has).<\/p>\n<p>The reason Active Directory search columns are so easy to write is the fact that we cheat: we typically don&rsquo;t explain the details behind such searches. Active Directory searches are actually pretty easy; after all, most of the code is boilerplate that doesn&rsquo;t need to change from script-to-script. However, there <i>is<\/i> a considerable amount of boilerplate code, and that requires explanation that goes beyond the scope of this little column. Therefore, we typically refer people to our two-part <i>Tales from the Script<\/i> series <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee692830.aspx\"><strong>Dude, Where&rsquo;s My Printer?<\/strong><\/a> if they need more information about writing scripts to search Active Directory.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EKD\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><strong>Note<\/strong>. You want to know how hard it is to be a Scripting Guy? Well, let&rsquo;s see. This particular Scripting Guy writes a column that simply refers people to previously-written material. And then he celebrates Thanksgiving by making his mom do all the cooking. A very tough life indeed.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>But we won&rsquo;t be totally lazy; at least we&rsquo;ll show you the code and explain how the query works. Here&rsquo;s a script that returns all the groups whose Name starts with <strong>G-S-Group<\/strong>:<\/p>\n<pre class=\"codeSample\"><span style=\"font-size: small\">On Error Resume Next\n\nConst ADS_SCOPE_SUBTREE = 2\n\nSet objConnection = CreateObject(\"ADODB.Connection\")\nSet objCommand =   CreateObject(\"ADODB.Command\")\nobjConnection.Provider = \"ADsDSOObject\"\nobjConnection.Open \"Active Directory Provider\"\nSet objCommand.ActiveConnection = objConnection\n\nobjCommand.Properties(\"Page Size\") = 1000\nobjCommand.Properties(\"Searchscope\") = ADS_SCOPE_SUBTREE \n\nobjCommand.CommandText = _\n    \"SELECT Name FROM 'LDAP:\/\/DC=fabrikam,DC=com' WHERE objectCategory='group' \" &amp; _\n        \"AND Name='G-S-Group*'\"\n\nSet objRecordSet = objCommand.Execute\n\nobjRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(\"Name\").Value\n    objRecordSet.MoveNext\nLoop<\/span><span style=\"font-size: small\">\n<\/span><\/pre>\n<p><span style=\"font-size: small\">The part we&rsquo;re interest<\/span>ed in is the SQL query that returns just those groups that meet our criteria (names starting with G-S-Group):<\/p>\n<pre class=\"codeSample\"><span style=\"font-size: small\">\"SELECT Name FROM 'LDAP:\/\/DC=fabrikam,DC=com' WHERE objectCategory='group' \" &amp; _\n        \"AND Name='G-S-Group*'\"<\/span>\n<\/pre>\n<p>As you can see, we need to specify two things in the Where clause: 1) the <strong>objectCategory<\/strong> must be equal to <i>group<\/i> (that way, we make sure we get back only groups and not users, OUs, printers, or anything else found in Active Directory); and 2) the <strong>Name<\/strong> attribute must begin with the letters <i>G-S-Group<\/i>. To specify the latter, we set the search value of Name to the desired string &#8211; <i>G-S-Group<\/i> &#8211; followed by an asterisk (*), the asterisk being short for &ldquo;anything.&rdquo; What we&rsquo;re saying here is pretty straightforward: &ldquo;Show me all the groups whose name starts with G-S-Group; I don&rsquo;t care what &#8211; if anything &#8211; comes after that.&rdquo;<\/p>\n<p>You can&rsquo;t get much more straightforward than that.<\/p>\n<p>If we wanted to search for groups whose Name <i>ended<\/i> in G-S-Group we&rsquo;d put the asterisk before the string:<\/p>\n<pre class=\"codeSample\"><span style=\"font-size: small\">Name='*G-S-Group'<\/span>\n<\/pre>\n<p>That&rsquo;s read as, &ldquo;Show me all the groups whose name <i>ends<\/i> with G-S-Group; I don&rsquo;t care what &#8211; if anything &#8211; comes before that.&rdquo; Or we could search for groups that have the string G-S-Group somewhere (anywhere) in their name. To do that, we put asterisks before and after the string:<\/p>\n<pre class=\"codeSample\"><span style=\"font-size: small\">Name='*G-S-Group*'<\/span>\n<\/pre>\n<p>All part of the fun &#8211; and the utility &#8211; of using scripts to search Active Directory.<\/p>\n<p>As for us, there&rsquo;s a turkey out there somewhere with our name on it. See you all again on Monday.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use wildcards to get a list of all the groups whose name starts with G-S-Group? &#8212; JD Hey, JD. It&rsquo;s the day before Thanksgiving, and one of the Scripting Guys is headed out of town as soon as he finishes his Hey, Scripting Guy! column. And what a coincidence: [&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,3,8,5],"class_list":["post-68463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching-active-directory","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use wildcards to get a list of all the groups whose name starts with G-S-Group? &#8212; JD Hey, JD. It&rsquo;s the day before Thanksgiving, and one of the Scripting Guys is headed out of town as soon as he finishes his Hey, Scripting Guy! column. And what a coincidence: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68463","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=68463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68463\/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=68463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}