# PowerShell Code that will list all local groups and their members

**URL:** <https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100>\
**Category:** PowerShell Help\
**Created:** [July 31, 2017, 3:06pm UTC](https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100 "2017-07-31T15:06:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 31, 2017, 3:06pm UTC](https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100/1 "2017-07-31T15:06:48Z")

</div>

Hi,

I’m looking for PowerShell Code that will export all local groups on a server and the users that belong to those groups. It would be helpful if this information can be exported to a .csv file or something readable. I did find some code but it displayed all the Domain Groups as well and that is not what I’m looking for. Only local groups and their members. This is for a Server migration project. Trying to avoid any manual work involving copying the user/Group information to the new server.

Thanks,

Kevin C.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [July 31, 2017, 3:12pm UTC](https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100/2 "2017-07-31T15:12:09Z")

</div>

Well, ignoring the SID issues, what about the WMI classes that represent users and groups?

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 1, 2017, 2:12am UTC](https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100/3 "2017-08-01T02:12:08Z")

</div>

This article may be of use for this

> **[Reporting on Local Groups in PowerShell -- Microsoft Certified Professional...](https://mcpmag.com/articles/2015/06/18/reporting-on-local-groups.aspx)**
>
> In any environment on your network, you have to be sure you know who had administrator rights on specific systems and also want a way to report on who (accounts or groups) that are a part the local groups on a system.

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [August 1, 2017, 7:23am UTC](https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100/4 "2017-08-01T07:23:13Z")

</div>

I found this in my code repository, haven’t tested it in a while but I know it worked at one point.

```
function Get-LocalGroups() {
  net localgroup | ?{ $_ -match "^\*.*" } | %{ $_.SubString(1) };
}

function Get-LocalGroupMembers() {
  param ([string]$groupName = $(throw "Need a name") )
  $lines = net localgroup $groupName
  $found = $false
  for ($i = 0; $i -lt $lines.Length; $i++ ) {
    if ( $found ) {
      if ( -not $lines[$i].StartsWith("The command completed")) {
        $lines[$i]
      }
    } elseif ( $lines[$i] -match "^----" ) {
      $found = $true;
    }
  }
}

Get-LocalGroups | % {Get-LocalGroupMembers $_}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/powershell-code-that-will-list-all-local-groups-and-their-members/9100/5 "2024-05-16T20:38:23Z")

</div>

Closed on May 16, 2024, 8:38pm UTC


