{"id":17391,"date":"2010-08-18T00:01:00","date_gmt":"2010-08-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/08\/18\/sort-an-active-directory-user-groups-members-by-using-powershell\/"},"modified":"2010-08-18T00:01:00","modified_gmt":"2010-08-18T00:01:00","slug":"sort-an-active-directory-user-groups-members-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/sort-an-active-directory-user-groups-members-by-using-powershell\/","title":{"rendered":"Sort an Active Directory User Group&#8217;s Members by Using PowerShell"},"content":{"rendered":"<p><b><\/b>\n<b><\/b>\n<b>Summary<\/b>: Sorting lists of data in VBScript often involved writing complex code. The <b>Sort<\/b> cmdlet in Windows PowerShell makes that task easier.\n&nbsp;\n<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\">\nHey, Scripting Guy! I need to be able to use Windows PowerShell to list the members of a group in Active Directory Domain Services (AD DS) in alphabetical order. The reason for this is I would like to compare the memberships of a couple of different groups, and if the members are not in alphabetical order, it makes the comparison more difficult.\n&#8212; TM\n<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\">\nHello TM,\nMicrosoft Scripting Guy Ed Wilson here. Today is a road trip day. My significant other and I woke before dawn and began our preparations for departure. It seems, at times, that NASA space missions are more impromptu than our road trips. We packed my laptop, power adapter, camera, a <a href=\"http:\/\/www.bing.com\/images\/search?q=horse+feed+bag&amp;FORM=BIFD\">feedbag<\/a>, and three Zunes (his, hers, and ours). We headed for open spaces.\nIt seems that at times the Windows PowerShell team deliberately made Windows PowerShell easy to use. This does not mean that Windows PowerShell is simple. It can get quite complex, but when you learn the basic concepts, you can do some really powerful things quite easily. Sorting is one of those really simple types of things.\nThe <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2004\/11\/22\/how-can-i-list-the-members-of-a-group-in-alphabetical-order.aspx\">How Can I List the Members of a Group in Alphabetical Order<\/a> Hey, Scripting Guy! Blog post from November 2004 illustrates one of the more common approaches to sorting data in VBScript&mdash;a bubble sort. (The other common technique was to use a <b>recordset<\/b> and <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/d39eb09e-77fc-495c-a506-ee40a8bedc2f\">is illustrated in this script<\/a> from the Scripting Guys Script Repository.)The VBScript from that article is shown here.<\/p>\n<blockquote><p>  <strong>SortGroupMembersAlphabetically.vbs<\/strong><\/p>\n<p><strong><\/strong><\/p>\n<div class=\"code\"><span style=\"color: #000000\">Dim<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">arrNames()<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">intSize<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Set<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objGroup<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">GetObject(<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/cn=hsgTestGroup,ou=HSG_TestOU,dc=nwtraders,dc=com&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">For<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Each<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">strUser<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objGroup.Member<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Set<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objUser<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">GetObject(<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/&#8221; <\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">strUser)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">ReDim<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Preserve<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">arrNames(intSize)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">arrNames(intSize)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objUser.CN<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">intSize<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">intSize<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Next<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">For<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(UBound(arrNames)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&#8211;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Step<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-1<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">For<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">j<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">i<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">UCase(arrNames(j))<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">UCase(arrNames(j+1))<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Then<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">strHolder<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">arrNames(j+1)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">arrNames(j+1)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">arrNames(j)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">arrNames(j)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">strHolder<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">End<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Next<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Next<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">For<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Each<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">strName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">arrNames<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Wscript.<\/span><span style=\"color: #0000ff\">Echo<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">strName<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Next<\/span> <\/div>\n<\/blockquote>\n<p>The<b> hsgTestGroup<\/b>, viewed in the Active Directory Users and Computers administration tool, is shown in the following image.\n<img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0818\/hsg-08-18-10-01.jpg\" alt=\"Image of hsgTestGroup\">\nWhen the SortGroupMembersAlphabetically.vbs script runs, the output appears that is shown in the following image. I know the picture says Windows PowerShell&mdash;this is actually one of my favorite tricks. I run VBScript inside Windows PowerShell. Because of the way the script is written, it would produce several popup dialog boxes, each of which would have to be individually closed. To avoid that, VBScript scripts that use <b>wscript.echo<\/b> to produce output are commonly run using Cscript. To do this, you normally open a command prompt, type cscript, and provide the path to the script. You can do the same thing using Windows PowerShell: Type cscript and provide the path to the script. I use the Windows PowerShell console for all my command-line needs, and I have not opened a command prompt for more than a year. If it is nothing else, Windows PowerShell is a better command prompt.\n<img decoding=\"async\" height=\"246\" width=\"600\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0818\/hsg-08-18-10-02.jpg\" alt=\"Image of output of SortGroupMembersAlphabetically.vbs script\">\nUsing the ADSI type accelerator, it requires two lines of code to retrieve the group membership and sort the list. The script first makes a connection to the group in Active Directory Domain Services (AD DS), and retrieves the <b>member<\/b> property. The members are piped to the <b>Foreach-Object<\/b> cmdlet where a connection is made to each of the groups. The result is piped to the <b>Sort-Object<\/b> cmdlet (alias is <b>sort<\/b>) and then the name is selected by the <b>Select-Object<\/b> cmdlet (alias is <b>select<\/b>). The Get-SortedGroupMembership.ps1 script is shown here.<\/p>\n<blockquote><p>  <strong>Get-SortedGroupMembership.ps1<\/strong><\/p>\n<p><span style=\"color: #000000\">([adsi]<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/cn=hsgTestGroup,ou=HSG_TestOU,dc=nwtraders,dc=com&#8221;<\/span><span style=\"color: #000000\">).member<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #0000ff\">ForEach-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{[adsi]<\/span><span style=\"color: #808080\">&#8220;LDAP:\/\/$_&#8221;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">sort<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">select<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">name<\/span> <\/p>\n<\/blockquote>\n<p>When the Get-SortedGroupMembership.ps1 script runs inside the Windows PowerShell ISE, the result appears that is shown in the following image.\n<img decoding=\"async\" height=\"434\" width=\"600\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0818\/hsg-08-18-10-03.jpg\" alt=\"Image of result of running Get-SortedGroupMembership.ps1 script in Windows PowerShell ISE\">\nOne reason for using the ADSI type accelerator would be if you do not have access to a domain that contains a domain controller that is running Windows Server 2008 R2.\nFor more information about the different approaches to working with AD DS, refer to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/08\/16\/using-powershell-to-find-all-disabled-users-in-active-directory.aspx\">Monday&rsquo;s Hey Scripting Guy! Blog post<\/a>.\nIf you do have access to a Windows Server 2008 R2, you have access to the Active Directory Windows PowerShell module. After that module is loaded, you can use the <b>Get-ADGroupMember<\/b> cmdlet to retrieve the group. The good thing about using the <b>Get-ADGroupMember<\/b> cmdlet is you do not have to type the entire distinguished name of the group as was done in the two previous scripts. After you have the group membership, you can sort by using the <b>Sort-Object<\/b> cmdlet, and even create a nice table with the <b>name<\/b>, <b>sid<\/b>, and <b>objectclass<\/b> of the object. The Windows PowerShell command is shown here:<\/p>\n<blockquote><p>  <span style=\"color: #000000\">Get-ADGroupMember<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">hsgtestgroup<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Recursive<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">sort<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">ft<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">name,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">sid,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objectClass<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-a<\/span> <\/p>\n<\/blockquote>\n<p>When the command runs, the output appears that is shown in the following image.\n<img decoding=\"async\" height=\"306\" width=\"600\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0818\/hsg-08-18-10-04.jpg\" alt=\"Image of output when command is run\">\nTM, that is all there is to using Windows PowerShell to sort the members of a group found in AD DS. User Management Week will continue tomorrow when we will talk about adding a domain user to the local administrators group.\nWe invite you follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum.<\/a>. See you tomorrow. Until then, peace.\n&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Sorting lists of data in VBScript often involved writing complex code. The Sort cmdlet in Windows PowerShell makes that task easier. &nbsp; Hey, Scripting Guy! I need to be able to use Windows PowerShell to list the members of a group in Active Directory Domain Services (AD DS) in alphabetical order. The reason for [&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,20,45],"class_list":["post-17391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching-active-directory","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Sorting lists of data in VBScript often involved writing complex code. The Sort cmdlet in Windows PowerShell makes that task easier. &nbsp; Hey, Scripting Guy! I need to be able to use Windows PowerShell to list the members of a group in Active Directory Domain Services (AD DS) in alphabetical order. The reason for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17391","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=17391"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17391\/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=17391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}