{"id":70513,"date":"2005-02-03T11:14:00","date_gmt":"2005-02-03T11:14:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/03\/how-can-i-determine-whether-or-not-a-group-has-any-members\/"},"modified":"2005-02-03T11:14:00","modified_gmt":"2005-02-03T11:14:00","slug":"how-can-i-determine-whether-or-not-a-group-has-any-members","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-whether-or-not-a-group-has-any-members\/","title":{"rendered":"How Can I Determine Whether or Not a Group Has Any Members?"},"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 check a computer and find out whether the Remote Desktop Users group has any members?<BR><BR>&#8212; ET<\/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, ET. Somewhat surprisingly, ADSI doesn\u2019t have any sort of <B>NumberOfMembers<\/B> property, a property that could tell you &#8211; at a glance &#8211; how many members are in a group. But that\u2019s all right, because we can simply iterate through the group\u2019s membership and count the members as we go. Here\u2019s a script that binds to the Remote Desktop Users group on the computer atl-ws-01 and then counts the number of users in that group:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;<\/p>\n<p>Set objGroup = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Remote Desktop Users&#8221;)\ni = 0\nFor Each objUser in objGroup.Members\n    i = i + 1\nNext<\/p>\n<p>Wscript.Echo &#8220;Number of users in group: &#8221; &amp; i\n<\/PRE>\n<P>If it\u2019s not immediately clear to you, here\u2019s how we count the number of members. We begin by setting a counter variable (<I>i<\/I>) to 0. We then use a For-Each loop to loop through the group membership. Each time we run through the loop, we increment the value of <I>i<\/I> by 1. Suppose we have 23 members in the group. In that case, we\u2019ll run through the loop 23 times and &#8211; that\u2019s right, at the end <I>i<\/I> will be equal to 23. We then echo the value of <I>i<\/I> (and, by extension, the number of members in the group).<\/P>\n<P>But what if the group doesn\u2019t <I>have<\/I> any members? In that case, we\u2019ll never run through the For-Each loop, and the value of <I>i <\/I>will remain at 0. Thus we\u2019ll end up echoing this message at the end of the script:<\/P><PRE class=\"codeSample\">Number of users in group: 0\n<\/PRE>\n<P>Of course, you didn\u2019t necessarily want to know the exact number of members in the Remote Desktop Users group; you just wanted to know if the group had <I>any<\/I> members. So here\u2019s a variation of the script. In this script, if we enter the For-Each loop (which we\u2019ll enter only if the group has at least one member) we echo the fact that the group has at least one member and then quit. If we don\u2019t enter the For-Each loop, then we echo the fact that the group doesn\u2019t have any members. In other words:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;<\/p>\n<p>Set objGroup = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Remote Desktop Users&#8221;)<\/p>\n<p>For Each objUser in objGroup.Members\n    Wscript.Echo &#8220;Remote Desktop Users has at least one member.&#8221;\n    Wscript.Quit\nNext<\/p>\n<p>Wscript.Echo &#8220;Remote Desktop Users does not have any members.&#8221;\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I check a computer and find out whether the Remote Desktop Users group has any members?&#8212; ET Hey, ET. Somewhat surprisingly, ADSI doesn\u2019t have any sort of NumberOfMembers property, a property that could tell you &#8211; at a glance &#8211; how many members are in a group. But that\u2019s all [&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":[23,24,3,5],"class_list":["post-70513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-local-accounts-and-windows-nt-4-0-accounts","tag-other-directory-services","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I check a computer and find out whether the Remote Desktop Users group has any members?&#8212; ET Hey, ET. Somewhat surprisingly, ADSI doesn\u2019t have any sort of NumberOfMembers property, a property that could tell you &#8211; at a glance &#8211; how many members are in a group. But that\u2019s all [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70513","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=70513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70513\/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=70513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}