Author Posts

July 31, 2017 at 8:36 pm

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.

July 31, 2017 at 8:42 pm

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

August 1, 2017 at 12:53 pm

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 $_}