{"id":365,"date":"2017-10-24T17:44:04","date_gmt":"2017-10-24T22:44:04","guid":{"rendered":"http:\/\/www.sqlkitten.com\/?p=365"},"modified":"2017-10-25T10:30:07","modified_gmt":"2017-10-25T15:30:07","slug":"powershell-we-haz-yur-active-directory-right-here","status":"publish","type":"post","link":"http:\/\/www.sqlkitten.com\/?p=365","title":{"rendered":"UPDATED: PowerShell &#8211; WE HAZ YUR ACTIVE DIRECTORY RIGHT HERE!"},"content":{"rendered":"<p>I discovered something last week &#8211; I had not blogged about little things that I thought I had blogged about. What the heck does that mean? It means that I tried to reference my blog for something because I thought &#8220;I totally blogged about that&#8221;&#8230;and found out that was not the case.<\/p>\n<p>Starting now, I am fixing this situation. There was something that popped up today that called for a PowerShell script and the <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/ee617193.aspx\">Get-ADGroupMember<\/a> cmdlet &#8211; get a list of users from a list of groups. Some users are in there more than once so this needs to be a distinct list, unless you are into manually cleaning up things like this, and then I will be sad for you. Because that is kinda sad.<\/p>\n<p>I originally wrote a script with two arrays (one for the initial list and one for the de-duped list of users), but even though this is quick and dirty, that was a little too dirty. Enter the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/group-object?view=powershell-5.1\">Group-Object<\/a> cmdlet &#8211; it takes this list of names and groups them. No black magic this time. Just a cmdlet, that comes baked into PowerShell giving me what I need.<\/p>\n<p>What? You wanted the code too? Oh, OK.<\/p>\n<pre class=\"lang:ps decode:true \">&lt;#\r\nName: activedirectory get list of users from multiple groups.ps1\r\nAuthor: Amy Herold\r\nDate: 24 October 2017\r\nPurpose: Get distinct list of users from multiple AD groups.\r\n\r\n#&gt;\r\n\r\n$groups = @(&#039;Admins&#039;,&#039;Managers&#039;,&#039;Some Other Group&#039;)\r\n$people = New-Object System.Collections.ArrayList;\r\n$people.Clear();\r\n\r\n#--------------get list of users from list of groups--------------\r\nforeach ($g in $groups)\r\n{\r\n\r\n$people.Add(@(Get-ADGroupMember -Identity $g | select name)) | Out-Null;\r\n}\r\n\r\n#------------use group-object and get a distinct list of names--------------\r\n$people.Name | Group-Object | select name<\/pre>\n<p>There you have it &#8211; quick, dirty and to the point. Enjoy. \ud83d\ude42<\/p>\n<p>UPDATE:\u00a0<a href=\"https:\/\/blog.iisreset.me\/\">Mathias Jessen<\/a> tweeted a one liner for this&#8230;.so no need for the one array! Woohoo!<\/p>\n<pre class=\"lang:ps decode:true \">(&#039;Admins&#039;,&#039;Managers&#039;,&#039;Some Other Group&#039; |Get-ADGroupMember |Group-Object -Property Name -NoElement).Name<\/pre>\n<p>I was trying to do this but was also just trying to get it done, and if in doubt, I slap things in arrays. Thanks Mathias!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I discovered something last week &#8211; I had not blogged about little things that I thought I had blogged about. What the heck does that mean? It means that I tried to reference my blog for something because I thought &hellip; <a href=\"http:\/\/www.sqlkitten.com\/?p=365\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[70,69,6,1],"tags":[57,23,27,22],"_links":{"self":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/365"}],"collection":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":5,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":370,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions\/370"}],"wp:attachment":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}