PowerShell Code that will list all local groups and their members

This topic contains 3 replies, has 4 voices, and was last updated by  Jon 2 weeks, 2 days ago.

  • Author
    Posts
  • #76291

    Kevin Conway
    Participant

    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.

  • #76294

    Don Jones
    Keymaster

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

  • #76304

    Simon B
    Participant
  • #76351

    Jon
    Participant

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

You must be logged in to reply to this topic.