Home > PowerShell > Powershell Get localgroup members on windows servers

Powershell Get localgroup members on windows servers

I wrote a script today to quickly know who all part of members of given group in a windows server. I felt it’s worth sharing with the global community given its use case. This script can be further enhanced to find out if a given user is part of the administrators group and for other requirements.

function get-localgroupmembers {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
$group,
[Parameter(Mandatory=$true)]
$computer
)

$MemberNames = @()

$objGroup= [ADSI]“WinNT://$computer/$Group,group”
$Members = @($objGroup.psbase.Invoke(“Members”))
$Members | ForEach-Object {
$MemberNames += $_.GetType().InvokeMember(“Name”, ‘GetProperty’, $null, $_, $null)
}
write-host “”
write-host “The following accounts are members of $group on $computer” -backgroundcolor yellow -foregroundcolor blue
write-host “”
Write-host $MemberNames
write-host “”

}

Example:

[PS]C:>Get-LocalGroupMembers -computer SERVER1 -group “Administrators”

[PS]C:>Get-LocalGroupMembers -computer SERVER1 -group “Power Users”

Hope this helps you

No related content found.

Categories: PowerShell
  1. Raheel
    October 1, 2011 at 2:19 am | #1

    Love it.

  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>