Skip to content

Get-LocalGroupMemberInfo

Windows: Lists members of a specified local security group

#Requires -Version 5.1
#Requires -Modules Microsoft.PowerShell.LocalAccounts

[CmdletBinding()]
Param
(
    [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
    [string]$Name,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process
{
    try
    {
        $scriptBlock = {
            Param($GroupName)
            Get-LocalGroupMember -Group $GroupName -ErrorAction Stop | ForEach-Object {
                [PSCustomObject]@{
                    Name         = $_.Name
                    PrincipalSource = $_.PrincipalSource
                    ObjectClass  = $_.ObjectClass
                    SID          = $_.SID.Value
                    ComputerName = $env:COMPUTERNAME
                }
            }
        }

        if ($ComputerName -ne $env:COMPUTERNAME)
        {
            $invokeParams = @{
                'ComputerName' = $ComputerName
                'ScriptBlock'  = $scriptBlock
                'ArgumentList' = $Name
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential)
            {
                $invokeParams.Add('Credential', $Credential)
            }

            $result = Invoke-Command @invokeParams
        }
        else
        {
            $result = &$scriptBlock -GroupName $Name
        }

        Write-Output $result
    }
    catch
    {
        throw
    }
}

Specifies the name of the local group to query (e.g., "Administrators").

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.