{"id":4466,"date":"2012-12-15T00:01:00","date_gmt":"2012-12-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/12\/15\/weekend-scripter-use-powershell-to-find-local-administrators-on-a-computer\/"},"modified":"2012-12-15T00:01:00","modified_gmt":"2012-12-15T00:01:00","slug":"weekend-scripter-use-powershell-to-find-local-administrators-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-find-local-administrators-on-a-computer\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Find Local Administrators on a Computer"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell and WMI CIM associations to find local administrators.\nMicrosoft Scripting Guy, Ed Wilson, is here. Well, we have been really lucky the past couple of days in Charlotte, North Carolina&mdash;at least weather wise. Yesterday, it was 60 degrees Fahrenheit and it was sunny with a clear blue sky. I am sitting on the lanai sipping a nice cup of green tea with a cinnamon stick, lemon grass, Jasmine flowers, and just a little bit of lavender. It tastes as great as it smells&mdash;certainly a nice way to relax and ease into the day.<\/p>\n<h2>Use WMI to find members of the local administrator group<\/h2>\n<p>When I can get away with it, I love simplicity. Once you know Windows Management Instrumentation (WMI), the world of Windows administration opens to you. In fact, with the introduction of the CIM cmdlets in Windows PowerShell&nbsp;3.0, and the movement towards <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2012\/07\/02\/omi-open-management-infrastructure-to-bring-the-power-of-powershell-and-standards-to-devices-in-your-datacenter.aspx\" target=\"_blank\">Open Management Infrastructure (OMI)<\/a>, knowing how to use this technology becomes much more important&mdash;it is knowledge you can leverage over and over in your daily work.\nAnyway, today I was playing around with association WMI classes, and I decided to spend a bit of time using the <strong>Win32_GroupUser <\/strong>WMI class.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;I talk about WMI associations in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/12\/13\/use-powershell-cim-cmdlets-to-discover-wmi-associations.aspx\" target=\"_blank\">Use PowerShell CIM Cmdlets to Discover WMI Associations<\/a><em>. <\/em>\nThis association class references two other classes: <strong>Win32_Group<\/strong> and <strong>Win32_Account<\/strong>. This information is shown here.<\/p>\n<p style=\"padding-left: 30px\">15:56 C:&gt; Get-CimClass win32_groupuser | select -expand cimclassproperties<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : GroupComponent<\/p>\n<p style=\"padding-left: 30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">CimType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Reference<\/p>\n<p style=\"padding-left: 30px\">Flags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Property, Key, ReadOnly, NullValue<\/p>\n<p style=\"padding-left: 30px\">Qualifiers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {Aggregate, read, key, MappingStrings&#8230;}<\/p>\n<p style=\"padding-left: 30px\">ReferenceClassName : Win32_Group<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : PartComponent<\/p>\n<p style=\"padding-left: 30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">CimType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Reference<\/p>\n<p style=\"padding-left: 30px\">Flags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Property, Key, ReadOnly, NullValue<\/p>\n<p style=\"padding-left: 30px\">Qualifiers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {read, key, MappingStrings, Override}<\/p>\n<p style=\"padding-left: 30px\">ReferenceClassName : Win32_Account\nBy using Windows PowerShell&nbsp;2.0 (or Windows PowerShell&nbsp;3.0), I can query this class by using the <strong>Get-WmiObject<\/strong> cmdlet to directly query the association class. I can then filter out the GroupComponent that matches <em>administrators. <\/em>For each of those, I can use the WMI type accelerator to retrieve the PartComponent property. From the output above, the PartComponent property contains the Win32_Account, and the GroupComponent property contains the Win32_Group, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_groupuser |<\/p>\n<p style=\"padding-left: 30px\">Where-Object { $_.GroupComponent -match &#8216;administrators&#8217; } |<\/p>\n<p style=\"padding-left: 30px\">ForEach-Object {[wmi]$_.PartComponent }\nWhen I run the code, the following appears in the Windows PowerShell console.<\/p>\n<p style=\"padding-left: 30px\">16:03 C:&gt; Get-WmiObject win32_groupuser |<\/p>\n<p style=\"padding-left: 30px\">&gt;&gt; Where-Object { $_.groupcomponent -match &#8216;administrators&#8217; } |<\/p>\n<p style=\"padding-left: 30px\">&gt;&gt; ForEach-Object {[wmi]$_.partcomponent }<\/p>\n<p style=\"padding-left: 30px\">&gt;&gt;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">AccountType : 512<\/p>\n<p style=\"padding-left: 30px\">Caption&nbsp;&nbsp;&nbsp;&nbsp; : edLTAdministrator<\/p>\n<p style=\"padding-left: 30px\">Domain&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : edLT<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-3464415469-1849125893-2015719117-500<\/p>\n<p style=\"padding-left: 30px\">FullName&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Administrator<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">AccountType : 512<\/p>\n<p style=\"padding-left: 30px\">Caption&nbsp;&nbsp;&nbsp;&nbsp; : edLTed<\/p>\n<p style=\"padding-left: 30px\">Domain&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : edLT<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-3464415469-1849125893-2015719117-1001<\/p>\n<p style=\"padding-left: 30px\">FullName&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ed<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Caption : IAMMREDDomain Admins<\/p>\n<p style=\"padding-left: 30px\">Domain&nbsp; : IAMMRED<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp; : Domain Admins<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-1457956834-3844189528-3541350385-512\nThe previous command is a single logical line, but it is broken at the pipe character for ease of reading. By using the Windows PowerShell&nbsp;3.0 syntax, and a few aliases, I can reduce this to a single physical line. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">gwmi win32_groupuser | ? groupcomponent -match &#8216;administrators&#8217; | % {[wmi]$_.partcomponent}<\/p>\n<h2>Use the PowerShell&nbsp;3.0 CIM cmdlets to get local admins<\/h2>\n<p>I can use the same WMI classes, but use the CIM cmdlets from Windows PowerShell&nbsp;3.0. This simplifies the code a bit. The first thing I need to do is to obtain a CIM instance. To do this, I use the <strong>Get-CimInstance<\/strong> cmdlet. I specify the WMI class as Win32_Group, and I look for groups with the name of <em>administrators<\/em>. I pipe the returned CIM Instance to the <strong>Get-AssociatedInstance<\/strong> cmdlet. This cmdlet will query for an association based upon the association class name.\nSo you see, it is important to know what WMI classes are made up on which WMI association class. I know, because I know how to use the CIM cmdlets to expand the output to see the association. Now, all I need to do is specify that I am looking for an association and specify the associated class, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance -ClassName win32_group -Filter &#8220;name = &#8216;administrators'&#8221; |<\/p>\n<p style=\"padding-left: 30px\">Get-CimAssociatedInstance -Association win32_groupuser\nThe command and its associated output is shown here.<\/p>\n<p style=\"padding-left: 30px\">16:06 C:&gt; Get-CimInstance -ClassName win32_group -Filter &#8220;name = &#8216;administrators'&#8221; |<\/p>\n<p style=\"padding-left: 30px\">&gt;&gt; Get-CimAssociatedInstance -Association win32_groupuser<\/p>\n<p style=\"padding-left: 30px\">&gt;&gt;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Caption&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AccountType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Domain<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">Administrator&nbsp;&nbsp;&nbsp; edLTAdminist&#8230; 512&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S-1-5-21-3464&#8230; edLT<\/p>\n<p style=\"padding-left: 30px\">ed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edLTed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 512&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;S-1-5-21-3464&#8230; edLT<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Caption : IAMMREDDomain Admins<\/p>\n<p style=\"padding-left: 30px\">Domain&nbsp; : IAMMRED<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp; : Domain Admins<\/p>\n<p style=\"padding-left: 30px\">SID&nbsp;&nbsp;&nbsp;&nbsp; : S-1-5-21-1457956834-3844189528-3541350385-512\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell and WMI CIM associations to find local administrators. Microsoft Scripting Guy, Ed Wilson, is here. Well, we have been really lucky the past couple of days in Charlotte, North Carolina&mdash;at least weather wise. Yesterday, it was 60 degrees Fahrenheit and it was sunny [&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":[362,3,4,61,45,6],"class_list":["post-4466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-3","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell and WMI CIM associations to find local administrators. Microsoft Scripting Guy, Ed Wilson, is here. Well, we have been really lucky the past couple of days in Charlotte, North Carolina&mdash;at least weather wise. Yesterday, it was 60 degrees Fahrenheit and it was sunny [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4466","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=4466"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4466\/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=4466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}