{"id":5265,"date":"2019-09-09T17:00:39","date_gmt":"2019-09-09T15:00:39","guid":{"rendered":"https:\/\/www.razumny.no\/?p=5265"},"modified":"2019-09-04T11:47:54","modified_gmt":"2019-09-04T09:47:54","slug":"building-a-script-to-find-all-members-of-a-given-group","status":"publish","type":"post","link":"https:\/\/www.razumny.no\/?p=5265","title":{"rendered":"Building a script to find all members of a given group"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Some time ago, I was asked to provide a list of everyone with access to a specific system. After communicating with the client, it transpired that they were particularly interested in knowing who were the members of a set of Active Directory groups. While this can be done manually, I wanted to try my hand at building a PowerShell-script that returned the information the client was asking for, and which I could reuse at some later point, as such requests pop up with some regularity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I started out with the <code>Get-ADGroupMember<\/code> command, followed by the group name in quotation marks, like so: <code>Get-ADGroupMember \"Active Directory Group Name\"<\/code>. Due to a scheme involving nested groups to grant access, this only gives me the group at the top, so this doesn&#8217;t get me all the way there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, then, we add the parameter -recursive, like so: <code>Get-ADGroupMember \"Active Directory Group Name\" -recursive<\/code>. This returns a list like this one:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>distinguishedName : CN=Administrator,CN=Users,DC=Fabrikam,DC=com<br>name              : Administrator<br>objectClass       : user<br>objectGUID        : 994f46e6-c62c-483f-a6cf-124197b6a959<br>SamAccountName    : Administrator<br>SID               : S-1-5-21-41432690-3719764436-1984117282-500<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>distinguishedName : CN=Sagiv Hadaya,CN=Users,DC=Fabrikam,DC=com<br>name              : Sagiv Hadaya<br>objectClass       : user<br>objectGUID        : 64706230-f179-4fe4-b8c9-f0d334e66ab1<br>SamAccountName    : SHadaya<br>SID               : S-1-5-21-41432690-3719764436-1984117282-1158<\/p><cite>Example result from the Get-ADGroupMember PowerShell documentation<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">In the naming scheme for the domain in question, the display name (see &#8220;name&#8221; in the list above) is set to be the same as their user name, so that is not super helpful. In addition, the only thing I need here is who the members are. As a result, I need to pipe the result of the command through a second command (<code>Get-ADUser<\/code>), returning the display name of each user. That looks like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Get-ADGroupMember \"GROUPNAME\" -recursive | Get-ADUser -property Displayname<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, too much information is returned. In addition to the results originally returned, this search also returns the fields DisplayName, Enabled, GivenName, Surname, and UserPrincipalName. Let&#8217;s pipe it through a filter (<code>select<\/code>) to return only the display name. That command looks like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Get-ADGroupMember \"GROUPNAME\" -recursive | Get-ADUser -property Displayname | select Displayname<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That returns the members of the group &#8211; in the PowerShell window, which looks like this: <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>DisplayName<br>&#8212;&#8212;&#8212;&#8211;<br>Administrator<br>Sagiv Hadaya <\/p><cite>Based on the above example result<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">I would like to have the result output to a text file, and so add another command. Here&#8217;s what that looks like:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Get-ADGroupMember \"GROUPNAME\" -recursive | Get-ADUser -property Displayname | select Displayname | Out-File -FilePath .\\PathOfFileHere.txt <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At this point, we have the overview needed in a handy file. Note that <code>-recursive<\/code> does not return groups, only users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, I was asked to provide a list of everyone with access to a specific system. After communicating with the client, it transpired that they were particularly interested in knowing who were the members of a set of Active Directory groups. While this can be done manually, I wanted to try my hand [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[615,820,677],"tags":[760,1186,1187,1188,969],"class_list":["post-5265","post","type-post","status-publish","format-standard","hentry","category-how-to-2","category-powershell","category-tips-tricks","tag-active-directory","tag-get-adgroupmember","tag-get-aduser","tag-out-file","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2ukSU-1mV","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6952,"url":"https:\/\/www.razumny.no\/?p=6952","url_meta":{"origin":5265,"position":0},"title":"Powershell: Add user to multiple groups","author":"Aleksander Nordgarden-R\u00f8dner","date":"24\/10\/2022","format":false,"excerpt":"A while back, I had a user that had to be added to a large number (150+) of active directory groups. Rather than doing so manually, I spent a little time looking up how to automate it, and created a script to help me do it. Here's how I constructed\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6605,"url":"https:\/\/www.razumny.no\/?p=6605","url_meta":{"origin":5265,"position":1},"title":"A better way of finding users&#8217; group memberships","author":"Aleksander Nordgarden-R\u00f8dner","date":"10\/01\/2022","format":false,"excerpt":"More than six years ago, I wrote a post, detailing how I could identify users' Active Directory group memberships. While the method I detailed certainly works, it isn't as simple as it might have been. Having recently found myself needing to perform that very same task again, I decided to\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7000,"url":"https:\/\/www.razumny.no\/?p=7000","url_meta":{"origin":5265,"position":2},"title":"PowerShell: Return multiple specific properties","author":"Aleksander Nordgarden-R\u00f8dner","date":"12\/12\/2022","format":false,"excerpt":"The script I described a few weeks ago for finding all members of several groups is serving me very well indeed. I was recently asked to provide both the display name and the login name for each member in the group. This was readily achieved by modifying the script slightly.\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4896,"url":"https:\/\/www.razumny.no\/?p=4896","url_meta":{"origin":5265,"position":3},"title":"Exporting AD group members&#8217; display name","author":"Aleksander Nordgarden-R\u00f8dner","date":"26\/11\/2018","format":false,"excerpt":"Five years ago, I showed you how to export a list of members of an Active Directory group, using a command line query. One issue I've run into using this query, is that I get their user name, not their actual name, which tends to make the resulting list hard\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5270,"url":"https:\/\/www.razumny.no\/?p=5270","url_meta":{"origin":5265,"position":4},"title":"PowerShell: Building a script to find all members of several given groups","author":"Aleksander Nordgarden-R\u00f8dner","date":"16\/09\/2019","format":false,"excerpt":"Last week, I showed you how I find all the nested members of a specified group. When we get requests for such audits, however, they usually specify doing so for a number of groups. Now, we could of course do it, one group at a time, adding information to our\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/www.razumny.no\/?cat=820"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6979,"url":"https:\/\/www.razumny.no\/?p=6979","url_meta":{"origin":5265,"position":5},"title":"Powershell: A script to find all members of several groups refined","author":"Aleksander Nordgarden-R\u00f8dner","date":"07\/11\/2022","format":false,"excerpt":"One of the scripts that I use most often is the one I wrote about back in September of 2019. Usually, I only run it for up to ten or so groups, so it's not a big problem to simply swap out the group name, rince and repeat. Recently, however,\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts\/5265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5265"}],"version-history":[{"count":6,"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts\/5265\/revisions"}],"predecessor-version":[{"id":5273,"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts\/5265\/revisions\/5273"}],"wp:attachment":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}