Skip to content

Get-LocalGroupInfo

Windows: Retrieves information about local security groups

#Requires -Version 5.1

[CmdletBinding()]
Param (
    [Parameter(ParameterSetName = 'ByName')]
    [string]$Name = "*",

    [Parameter(Mandatory = $true, ParameterSetName = 'BySID')]
    [string]$SID,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process {
    try {
        $scriptBlock = {
            Param($GroupName, $GroupSID, $SetName)
            if ($SetName -eq 'BySID') {
                Get-LocalGroup -SID $GroupSID -ErrorAction Stop | Select-Object Name, SID, Description
            }
            else {
                Get-LocalGroup -Name $GroupName -ErrorAction Stop | Select-Object Name, SID, Description
            }
        }

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

            $result = Invoke-Command @invokeParams
        }
        else {
            $result = &$scriptBlock -GroupName $Name -GroupSID $SID -SetName $PSCmdlet.ParameterSetName
        }

        $output = foreach ($g in $result) {
            [PSCustomObject]@{
                Name         = $g.Name
                SID          = $g.SID.Value
                Description  = $g.Description
                ComputerName = $ComputerName
            }
        }

        Write-Output ($output | Sort-Object Name)
    }
    catch {
        throw
    }
}

Specifies the name of the local group to retrieve. Supports wildcards. Defaults to all groups.

Specifies the security ID (SID) of the local group to retrieve.

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.