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