Skip to content

Invoke-MgmtGraphGroupOwnerAction

MgmtGraph: Manages owner actions for a Microsoft Graph group

#Requires -Version 5.1
#Requires -Modules Microsoft.Graph.Groups

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$Identity,

    [string[]]$AddOwner,

    [string[]]$RemoveOwner
)

Process {
    try {
        # Handle Additions
        foreach ($id in $AddOwner) {
            $params = @{
                'GroupId'          = $Identity
                'OdataId'          = "https://graph.microsoft.com/v1.0/directoryObjects/$id"
                'Confirm'          = $false
                'ErrorAction'      = 'Stop'
            }
            New-MgGroupOwnerByRef @params
        }

        # Handle Removals
        foreach ($id in $RemoveOwner) {
            $params = @{
                'GroupId'          = $Identity
                'DirectoryObjectId' = $id
                'Confirm'          = $false
                'ErrorAction'      = 'Stop'
            }
            Remove-MgGroupOwnerByRef @params
        }

        $result = [PSCustomObject]@{
            GroupId      = $Identity
            AddedCount   = ($AddOwner | Measure-Object).Count
            RemovedCount = ($RemoveOwner | Measure-Object).Count
            Action       = "GroupOwnerActionExecuted"
            Status       = "Success"
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

Specifies the ID of the group to manage.

Optional. Specifies an array of User or Service Principal IDs to add as owners.

Optional. Specifies an array of User or Service Principal IDs to remove from ownership.

An interactive directory of PowerShell scripts.