Skip to content

Add-MgmtGraphGroupToLifecyclePolicy

MgmtGraph: Adds a group to a lifecycle policy

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

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

    [string]$GroupLifecyclePolicyId
)

Process {
    try {
        $policyId = $GroupLifecyclePolicyId
        if (-not $policyId) {
            $policies = Get-MgGroupLifecyclePolicy -ErrorAction Stop
            if (-not $policies) {
                throw "No active group lifecycle policies found in the tenant."
            }
            $policyId = $policies[0].Id
        }

        $params = @{
            'GroupId'                 = $GroupId
            'GroupLifecyclePolicyId' = $policyId
            'ErrorAction'             = 'Stop'
        }

        $success = Add-MgGroupToLifecyclePolicy @params
        
        $result = [PSCustomObject]@{
            GroupId                = $GroupId
            GroupLifecyclePolicyId = $policyId
            Added                  = $true
            Timestamp              = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

The unique identifier of the Microsoft Graph group.

Optional. The unique identifier of the lifecycle policy. If not specifies, the script will automatically discover and use the first available policy in the tenant.

An interactive directory of PowerShell scripts.