PowerShell Code that will list all local groups and their members


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.


Kevin C.

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

This article may be of use for this

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")) {
    } elseif ( $lines[$i] -match "^----" ) {
      $found = $true;

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