Skip to content

New-LocalGroupRemote

Windows: Creates a new local security group on a computer

#Requires -Version 5.1

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

    [string]$Description,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process {
    try {
        $scriptBlock = {
            Param($GroupName, $GroupDesc)
            $params = @{
                'Name' = $GroupName
                'Confirm' = $false
                'ErrorAction' = 'Stop'
            }
            if ($GroupDesc) { $params.Add('Description', $GroupDesc) }
            
            New-LocalGroup @params
            Get-LocalGroup -Name $GroupName | Select-Object Name, SID, Description
        }

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

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

        $output = [PSCustomObject]@{
            Name         = $result.Name
            SID          = $result.SID.Value
            Description  = $result.Description
            ComputerName = $ComputerName
            Action       = "GroupCreated"
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $output
    }
    catch {
        throw
    }
}

Specifies the name of the new local group.

Specifies a description for the new group.

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.