Skip to content

Invoke-MgmtGraphGroupMemberAction

MgmtGraph: Manages membership 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[]]$AddMember,

    [string[]]$RemoveMember
)

Process {
    try {
        # Handle Additions
        foreach ($id in $AddMember) {
            $null = New-MgGroupMember -GroupId $Identity -DirectoryObjectId $id -ErrorAction Stop
        }

        # Handle Removals
        foreach ($id in $RemoveMember) {
            $null = Remove-MgGroupMemberByRef -GroupId $Identity -DirectoryObjectId $id -ErrorAction Stop
        }

        $result = [PSCustomObject]@{
            GroupId      = $Identity
            AddedCount   = ($AddMember | Measure-Object).Count
            RemovedCount = ($RemoveMember | Measure-Object).Count
            Action       = "GroupMemberActionExecuted"
            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 Group IDs to add as members.

Optional. Specifies an array of User or Group IDs to remove from memberships.

An interactive directory of PowerShell scripts.