Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Local Group Members with PowerShell

Posted on February 17, 2016

Recently I posted a function to get information about local user accounts. I received a lot of positive feedback so it seemed natural to take this the next step and create a similar function to enumerate or list members of a local group, such as Administrators.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The function, Get-LocalGroupMember, also relies on ADSI and is similar to my local user function so I won't repeat the details.  Since I assume most of the time the only local group that matters is Administrators I made that the default.  I also set the computer default to the local host. But it is simple enough to query another computer.

image

You can pipe in computer names and use the object properties to do additional filtering, sorting or grouping.

"chi-fp02","chi-core01","chi-hvr1","chi-hvr2","chi-web02" | 
get-localgroupmember |
where {$_.IsLocal -AND $_.name -ne 'Administrator'} |
Select Computername,Class,Name

image

In this example I wanted to search a group of computers and identify local members of Administrators that were not the Administrator account.

As with my previous function, I think you'll find it better to use PowerShell remoting if you plan on querying multiple remote servers or need to use alternate credentials. You can read function help for more details.

You can always find the most current version of the function in my Github library.

I hope you find this useful. Comments are welcome here, but please post bugs or suggestions on GitHub.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

7 thoughts on “Get Local Group Members with PowerShell”

  1. YP Chien says:
    February 17, 2016 at 12:51 pm

    Get-LocalGroupMember -Computername “mycomputerN”
    foreach : Error while invoking GetType. Could not find member.
    At line:176 char:39
    + $group.psbase.invoke(“Members”) | foreach {
    + ~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [ForEach-Object], MissingMemberException
    + FullyQualifiedErrorId : System.MissingMemberException,Microsoft.PowerShell.Commands.ForEachObjectCommand

    1. Jeffery Hicks says:
      February 17, 2016 at 1:12 pm

      That is odd. I would expect if there was an error connecting to the computer or group you wouldn’t get that far. Are you querying a group on the local computer or a remote? What version of PowerShell is running?

  2. D. Stein says:
    February 17, 2016 at 9:33 pm

    If I run this and enter “Administrators” for the group name it errors out. The group contains Administrator and my user account (2 users). The error is below. Let me know if I overlooked something when trying to run it?

    foreach : Error while invoking GetType. Could not find member.
    At C:\Scripts\PowerShell\Get-LocalGroupMember.ps1:146 char:39
    + $group.psbase.invoke(“Members”) | foreach {
    + ~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [ForEach-Object], MissingMemberException
    + FullyQualifiedErrorId : System.MissingMemberException,Microsoft.PowerShell.Commands.ForEachObjectCommand

    1. Jeffery Hicks says:
      February 17, 2016 at 9:51 pm

      What version of PowerShell are you running this on? What is the OS on the remote computer?

    2. Jeffery Hicks says:
      February 17, 2016 at 10:04 pm

      The only other thing I can think of is that there is some sort of timeout happening trying to invoke the COM objects. Does the command work on the local computer?

  3. Jeffery Hicks says:
    February 18, 2016 at 10:27 am

    Since the original post, I’ve updated the function on GitHub to v1.5. Hoping it handles these unusual errors better although I’m still not sure what is causing them.

  4. Jeffery Hicks says:
    February 18, 2016 at 10:53 am

    Or take a look at version 2 http://bit.ly/1QmYFQO

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d