{"id":65463,"date":"2007-02-22T00:59:00","date_gmt":"2007-02-22T00:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/22\/how-can-i-return-information-for-each-member-in-a-group\/"},"modified":"2007-02-22T00:59:00","modified_gmt":"2007-02-22T00:59:00","slug":"how-can-i-return-information-for-each-member-in-a-group","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-return-information-for-each-member-in-a-group\/","title":{"rendered":"How Can I Return Information For Each Member in a Group?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I write a script that, for each member in a group, returns the member\u2019s name, department, address, title, and description?<BR><BR>&#8212; TL-V<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TL-V. You <I>would<\/I> have to mention the word \u201cscript,\u201d wouldn\u2019t you? Usually that wouldn\u2019t bother us too much, but in the past 9 days or so the Scripting Guy who writes this column has looked at, tested, and recorded scores for 2,000 or so scripts, all as part of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Winter Scripting Games<\/B><\/A>. Likewise, the Scripting Guy who doesn\u2019t write this column has looked at, tested, and recorded scores for 2,000 or so scripts herself. Plus we still have several hundred more scripts that <I>haven\u2019t<\/I> been looked at, tested, or had their scores recorded. To tell you the truth, we\u2019re all scripted out, TL-V; if we ever see another script again it\u2019ll be too soon.<\/P>\n<P>But don\u2019t worry. After all, there are plenty of places you can turn to for help; it\u2019s not like we\u2019re the only daily scripting column here on TechNet. Why there\u2019s \u2026 and there\u2019s \u2026 um \u2026. <\/P>\n<P>Hmmm \u2026.<\/P>\n<P>OK, fine, we\u2019ll help you out, just this once. But if anyone asks where you got this script, whatever you do <I>don\u2019t tell them<\/I>. The last thing we Scripting Guys need is a bunch of people asking us questions about scripting.<\/P>\n<TABLE class=\"dataTable\" id=\"ERD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. OK, forget that last part. The Scripting Editor, who recently bought a new car, just reminded us that we <I>do<\/I> need a bunch of people asking us questions about scripting. We think she said something about job security, but, if you want to know the truth, we weren\u2019t paying that much attention; she <I>does<\/I> ramble on and on, you know. And if you\u2019ve ever read <I>Hey, Scripting Guy!<\/I> then you know how much we dislike people who ramble on and on without ever getting to the point.<\/P>\n<P>Which reminds us of a funny story involving the Scripting Son and a baseball game \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, here\u2019s a script that can return information about all the members of a group:<\/P><PRE class=\"codeSample\">Set objGroup = GetObject(&#8220;LDAP:\/\/CN=Managers,OU=Finance,DC=fabrikam,DC=com&#8221;)<\/p>\n<p>For Each objUser in objGroup.Members\n    Wscript.Echo &#8220;Name: &#8221; &amp; objUser.DisplayName\n    Wscript.Echo &#8220;Department: &#8221; &amp; objUser.department\n    Wscript.Echo &#8220;Street address: &#8221; &amp; objUser.streetAddress\n    Wscript.Echo &#8220;Title: &#8221; &amp; objUser.title\n    Wscript.Echo &#8220;Description: &#8221; &amp; objUser.description\n    Wscript.Echo\nNext\n<\/PRE>\n<P>As you can see, there\u2019s not much to this script. We start out by binding to the desired group in Active Directory; in our sample script, that\u2019s the Managers group, which resides in the Finance OU of fabrikam.com:<\/P><PRE class=\"codeSample\">Set objGroup = GetObject(&#8220;LDAP:\/\/CN=Managers,OU=Finance,DC=fabrikam,DC=com&#8221;)\n<\/PRE>\n<P>As long as we\u2019re on the subject, one question we get asked over and over again is this: how can I bind to a group that resides in a child OU of another OU?. In other words, how can I bind to a group that resides in the Accounting OU, which, in turn, resides in the Finance OU? We\u2019ve addressed that question before, but it\u2019s worth repeating here: you simply include both OUs in your binding string. Something like this:<\/P><PRE class=\"codeSample\">Set objGroup = GetObject(&#8220;LDAP:\/\/CN=Managers,OU=Accounting,OU=Finance,DC=fabrikam,DC=com&#8221;)\n<\/PRE>\n<P>The trick here is to always work from back to front when creating your binding string. The object\u2019s CN always comes first, followed by the child OU, the parent OU, and then the domain information. Suppose the account lived in the Payables OU, which was a child OU of Accounting, which, of course, is a child of the Finance OU. That\u2019s fine: start with Payables, tack on Accounting, then continue on through Finance:<\/P><PRE class=\"codeSample\">Set objGroup = GetObject(&#8220;LDAP:\/\/CN=Managers,OU=Payables,OU=Accounting,OU=Finance,DC=fabrikam,DC=com&#8221;)\n<\/PRE>\n<P>OK, now back to our regularly-scheduled program. As it turns out, one of the properties of an Active Directory group is the <B>Members<\/B> attribute; as the name implies, this attribute contains information about all the members in the group. Whoa, wait a second: don\u2019t just try echoing the value of the Members attribute. Why not? Because Members happens to be a multi-valued attribute, an attribute that can contain more than one value. (Which makes sense; after all, groups typically contain more than one member.) Any time you\u2019re dealing with a multi-valued attribute you need to set up a For Each loop in order to walk through all the values in that attribute (in this case, all the members of the group). That\u2019s why we have this line of code:<\/P><PRE class=\"codeSample\">For Each objUser in objGroup.Members\n<\/PRE>\n<P>And <I>then<\/I>, once we\u2019re inside the loop, we can echo back the desired information for each group member:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Name: &#8221; &amp; objUser.DisplayName\nWscript.Echo &#8220;Department: &#8221; &amp; objUser.department\nWscript.Echo &#8220;Street address: &#8221; &amp; objUser.streetAddress\nWscript.Echo &#8220;Title: &#8221; &amp; objUser.title\nWscript.Echo &#8220;Description: &#8221; &amp; objUser.description\n<\/PRE>\n<P>And that should do it, TL-V. When all is said and done you\u2019ll get back information similar to this for each user listed as a group member:<\/P><PRE class=\"codeSample\">Name: Ken Myer\nDepartment: Accounts Receivable\nStreet address: Building 8, Room 435 \nTitle: Senior Account Manager\nDescription: Accounts Receivable manager\n<\/PRE>\n<P>You know, that\u2019s a good point: if the Scripting Guys already have several hundred unscored Scripting Games entries lying around, well, what difference <I>would<\/I> a few more make? In other words, if you haven\u2019t entered the Scripting Games yet there\u2019s still time to complete events 9 and\/or 10. Submit a script for at least one of those events and you\u2019ll be eligible to win a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><B>Dr. Scripto Bobblehead doll<\/B><\/A> or a copy of the book <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/payette1.mspx\"><B>Windows PowerShell in Action<\/B><\/A>. More important, you\u2019ll be giving the Scripting Guys an opportunity to do what they love best.<\/P>\n<P>Oh, wait, never mind. Instead, you\u2019ll be giving us a chance to look at, test, and record scores for scripts, won\u2019t you?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write a script that, for each member in a group, returns the member\u2019s name, department, address, title, and description?&#8212; TL-V Hey, TL-V. You would have to mention the word \u201cscript,\u201d wouldn\u2019t you? Usually that wouldn\u2019t bother us too much, but in the past 9 days or so the Scripting [&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,3,20,5],"class_list":["post-65463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-groups","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I write a script that, for each member in a group, returns the member\u2019s name, department, address, title, and description?&#8212; TL-V Hey, TL-V. You would have to mention the word \u201cscript,\u201d wouldn\u2019t you? Usually that wouldn\u2019t bother us too much, but in the past 9 days or so the Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65463","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=65463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65463\/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=65463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}