Functions/Remove-IAGroup.ps1

Function Remove-IAGroup {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, ParameterSetName = 'IAGroup', ValueFromPipeline = $true)]
        [PSObject] $IAGroup,
        [Parameter(Mandatory = $true, ParameterSetName = 'Id', ValueFromPipelineByPropertyName = $true)]
        [Guid] $Id
    )

    Process {
        if($IAGroup){
            foreach($group in $IAGroup){
            
                # Delete Widgets first
                $WidgetToBeDeleted = Get-IAWidget -All -Expand | Where-Object { $_.Group.Id -like $group.Id }

                foreach($widget in $WidgetToBeDeleted){

                    Remove-IAWidget -Id $widget.Id
                }

                $Uri = "Groups($($group.Id))"
                $response = Invoke-IAQuery -QueryUrl $Uri -Method Delete
                
                if ($null -eq $response.value) {
                    return $null
                }

                return $response.value
            }
        }

        if($Id){
            foreach($item in $Id){

                $WidgetToBeDeleted = Get-IAWidget -All -Expand | Where-Object { $_.Group.Id -like $item }

                foreach($widget in $WidgetToBeDeleted){
                    Remove-IAWidget -Id $widget.Id
                }
    
                $Uri = "Groups($item)"
                $response = Invoke-IAQuery -QueryUrl $Uri -Method Delete

                if ($null -eq $response.value) {
                    return $null
                }

                return $response.value
            }
        }
    }
}