Francis Padron
Francis Padron

Reputation: 327

Powershell 2.0 Get users for a local group

How can I get user list from a local group? I only have PS 2.0 and it does not have Get-ADGroup command.

I can get local groups:

$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$groups = $adsi.Children | Where { $_.SchemaClassName -eq 'Group' }
$group | ft Name

What I need is to list all the members for each group.

Upvotes: 0

Views: 5120

Answers (2)

JPBlanc
JPBlanc

Reputation: 72680

You can try the following

$obj = [ADSI]"WinNT://$env:COMPUTERNAME"
$admingroup = $obj.Children | Where { $_.SchemaClassName -eq 'group'} |  where {$_.name -eq 'Administrators'}
$admingroup.Invoke('Members') | % {$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)}
$admingroup.Invoke('Members') | % {$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)}

Here are the common properties

String :

Description, FullName, HomeDirectory, HomeDirDrive, Profile, LoginScript, ObjectSID

Integer :

UserFlags, PasswordExpired, PrimaryGroupID

Time :

PasswordAge

You'll find more in Microsoft documentation.

Upvotes: 1

Nitesh
Nitesh

Reputation: 874

Try this

$computer = [ADSI]"WinNT://$env:COMPUTERNAME"

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

This doesn't give the domain though, hence i had to look for other ways, like:

If you want to see members of a local group quickly:

PS C:\> net localgroup USERS
Alias name     USERS
Comment        Users are prevented from making accidental or intentional system-wide changes and can run most applications

Members

-------------------------------------------------------------------------------
NT AUTHORITY\Authenticated Users
NT AUTHORITY\INTERACTIVE
The command completed successfully.

Now you can manipulate this output a bit to get what you need:

$computer = [ADSI]"WinNT://$env:COMPUTERNAME"

$groups = $computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | select -ExpandProperty Name

Foreach($group in $groups)
 {
  write-host $group
  write-host "------"
  net localgroup $group | where {$_ -notmatch "command completed successfully"} | select -skip 6
  Write-host
 }

Upvotes: 0

Related Questions