{"id":11801,"date":"2011-12-13T00:01:00","date_gmt":"2011-12-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/12\/13\/use-powershell-to-search-active-directory-for-gids\/"},"modified":"2011-12-13T00:01:00","modified_gmt":"2011-12-13T00:01:00","slug":"use-powershell-to-search-active-directory-for-gids","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-search-active-directory-for-gids\/","title":{"rendered":"Use PowerShell to Search Active Directory for GIDs"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to search Active Directory for GIDs.<\/p>\n<p>Today we have as our guest blogger, Chris Wu.<\/p>\n<p style=\"padding-left: 30px\">Chris Wu&rsquo;s career at Microsoft started in 2002, first as a support engineer in Microsoft Global Technical Support Center in China to support various components of the base operating system. Now he is a premier field engineer in Canada, and he specializes in platform and development support. During the course of troubleshooting, performance tuning, and debugging, he has created many utilities to ease and facilitate the process by leveraging various programming languages, like C, C++, and C#. Now Windows PowerShell has become his new favorite.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0434.Chris.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0434.Chris.jpg\" alt=\"Photo of Chris Wu\" title=\"Photo of Chris Wu\" \/><\/a><\/p>\n<p>Take it away Chris&#8230;<\/p>\n<p>So what is a GID? To the UNIX (and Linux) operating systems, group identifiers (GIDs) and user identifiers (UIDs) are what security identifiers (SIDs) are to the Windows operating system. Each user in the UNIX system has a UID and a primary GID to identify the primary group he belongs to. Users can also belong to multiple secondary groups to inherit security settings.<\/p>\n<p>In a pure Windows world, these fields are not used, even though the <b>uidNumber<\/b> and <b>gidNumber<\/b> attributes are available for compliance to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/site\/ms675718\" target=\"_blank\">LDAP protocol<\/a>. The following image shows that the <b>gidNumber <\/b>attribute is not set.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5074.01.png\"><img decoding=\"async\" border=\"0\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5074.01.png\" \/><\/a>&nbsp;<\/p>\n<p>In a mixed Windows and UNIX environment, however, the <b>uidNumber<\/b> and <b>gidNumber<\/b> attributes provide a mechanism to map UNIX users and groups to Windows names. For example, after <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc753302(WS.10).aspx\" target=\"_blank\">Services for NFS<\/a> are installed, Active Directory lookup services utilize these attributes for identity management.<\/p>\n<p>So how can we query groups that have GIDs assigned? As always, there are multiple ways to accomplish this in the Windows operating system. Windows PowerShell has made searching through LDAP much easier by implementing the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/08\/24\/use-the-powershell-adsisearcher-type-accelerator-to-search-active-directory.aspx\" target=\"_blank\">[adsisearcher] type accelerator<\/a>, which instantiates a System.DirectoryServices.DirectorySearcher .NET object from a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa746475(v=vs.85).aspx\" target=\"_blank\">search filter<\/a>.<\/p>\n<p>As a start, we can use <b>&#8220;(objectCategory=group)&#8221;<\/b> as the filter string to enumerate Active Directory groups as shown here.<\/p>\n<p style=\"padding-left: 30px\">$searcher=[adsisearcher]&#8221;(objectCategory=group)&#8221;<\/p>\n<p style=\"padding-left: 30px\">$result = $searcher.FindAll()<\/p>\n<p style=\"padding-left: 30px\">$result | Select-Object<\/p>\n<p>This script and its associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7028.02.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7028.02.png\" \/><\/a><\/p>\n<p>This simple search filter uses the <b>&#8220;(&lt;attribute&gt;&lt;operator&gt;&lt;value&gt;)&#8221;<\/b> format that is mentioned in the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc753302(WS.10).aspx\" target=\"_blank\">Services for NFS Step-by-Step Guide<\/a>. Now to find all groups that have the <b>gidNumber<\/b> attribute set, we need another filter that checks this attribute, and <b>&#8220;(gidNumber=*)&#8221;<\/b> will do the trick. By combining the two filters with the <b>&#8220;(&lt;operator&gt;&lt;filter1&gt;&lt;filter2&gt;)&#8221;<\/b> format, we get the following:<\/p>\n<p style=\"padding-left: 30px\">&#8220;(&amp;(objectCategory=group)(gidNumber=*))&#8221;<\/p>\n<p>This script and its associated output follow.&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$searcher=[adsisearcher]&#8221;(&amp;(objectCategory=group)(gidNumber=*))&#8221;<\/p>\n<p style=\"padding-left: 30px\">$result = $searcher.FindAll()<\/p>\n<p style=\"padding-left: 30px\">$result | Select-Object @{Name=&#8221;DN&#8221;;Expression={$_.properties.distinguishedname}},@{Name=&#8221;gid&#8221;;Expression={$_.properties.gidnumber }}<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3113.03.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3113.03.png\" \/><\/a>&nbsp;<\/p>\n<p>The previous script would have worked just fine if <b>objectClass <\/b>was used in place of <b>objectCategory<\/b>. An interesting comparison between the two is documented in this <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa746407(v=vs.85).aspx\" target=\"_blank\">Windows Dev Center topic<\/a>.<\/p>\n<p>One can easily append an <b>Export-Csv<\/b> cmdlet at the end of the previous command to save the result as a .csv file for reading or processing in the future.<\/p>\n<p>With the Active Directory module that&rsquo;s available for Windows&nbsp;7 or Windows&nbsp;2008&nbsp;R2, there are multiple ways to achieve the same goal by using a single cmdlet. Two options are shown here.&nbsp;<\/p>\n<p style=\"padding-left: 30px\">#Use LDAP search filter syntax<\/p>\n<p style=\"padding-left: 30px\">Get-ADObject -LDAPFilter &#8220;(&amp;(objectCategory=group)(gidNumber=*))&#8221; -Properties gidNumber | Select @{Name=&#8221;DN&#8221;;Expression={$_.DistinguishedName}},@{Name=&#8221;gid&#8221;;Expression={$_.gidNumber}}<\/p>\n<p style=\"padding-left: 30px\">#Or, use AD filter syntax<\/p>\n<p style=\"padding-left: 30px\">Get-ADGroup -filter &#8216;gidNumber -like &#8220;*&#8221;&#8216; -Properties gidNumber | Select @{Name=&#8221;DN&#8221;;Expression={$_.DistinguishedName}},@{Name=&#8221;gid&#8221;;Expression={$_.gidNumber}}<\/p>\n<p>This command output is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7266.04.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7266.04.png\" \/><\/a><\/p>\n<p><b>Note<\/b>: The <i>Properties<\/i> parameter is needed in both commands to include the <b>gidNumber<\/b> property (which is not exported by default) in the resultant objects.<\/p>\n<p>We can actually query Active Directory objects through WMI. The <b>DS_GROUP<\/b> class under the <b>&#8220;root\\directory\\ldap&#8221;<\/b> namespace instantiates all group objects from Active Directory. So with a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa394606(v=vs.85).aspx\">WQL<\/a> filter, we can retrieve a list of groups with the <b>gidNumber<\/b> attribute defined as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -namespace root\\directory\\ldap -class ds_group -filter &#8216;DS_gidNumber IS NOT NULL&#8217; | Select-Object @{Name=&#8221;DN&#8221;;Expression={$_.DS_distinguishedName}},@{Name=&#8221;gid&#8221;;Expression={$_.DS_gidnumber }}<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0068.05.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0068.05.png\" \/><\/a><\/p>\n<p>Notice how WMI uses the <b>&#8220;DS_&#8221;<\/b> prefix for both the <b>distinguishedName<\/b> and <b>gidNumber<\/b> attributes.<\/p>\n<p>Now with all these options available, which route has the best performance? This question will be left to the readers to test in a real environment. As a hint, <b>Measure-Command<\/b> is the cmdlet to assess the execution time of script blocks.<\/p>\n<p>Thank you CHris for sharing your time and knowledge with our readers.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to search Active Directory for GIDs. Today we have as our guest blogger, Chris Wu. Chris Wu&rsquo;s career at Microsoft started in 2002, first as a support engineer in Microsoft Global Technical Support Center in China to support various components of the base operating system. Now he is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,302,56,3,303,45],"class_list":["post-11801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-chris-wu","tag-guest-blogger","tag-scripting-guy","tag-searching","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to search Active Directory for GIDs. Today we have as our guest blogger, Chris Wu. Chris Wu&rsquo;s career at Microsoft started in 2002, first as a support engineer in Microsoft Global Technical Support Center in China to support various components of the base operating system. Now he is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11801","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11801\/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=11801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}