Users and Local Groups Report using Powershell? Users and Local Groups Report using Powershell? powershell powershell

Users and Local Groups Report using Powershell?


In fact you can with the ADSI type shortcut and the WinNT moniker. Here's an example to list groups and members from your own machine:

$server="."$computer = [ADSI]"WinNT://$server,computer"$computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach {    write-host $_.name    write-host "------"    $group =[ADSI]$_.psbase.Path    $group.psbase.Invoke("Members") | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}    write-host}


Powershell does not have any inherent support for such a feature. However it's easy to wrap the "net localgroup" command with a couple of powershell functions and thus enable it in the pipeline.

Get Local Groups

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

Get Local Group members

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;    }  }}


Below is an improved version of Shay Levy's script which works for local groups with "orphaned" accounts which SIDs can't be resolved.

$server = "$env:COMPUTERNAME"$computer = [ADSI]"WinNT://$server,computer"$computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach {    write-host $_.name    write-host "------"    $group =[ADSI]$_.psbase.Path    $group.psbase.Invoke("Members") | foreach {$_."GetType".Invoke().InvokeMember("Name", 'GetProperty', $null, $_, $null)}    write-host}