Functions/Remove-IAView.ps1

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

    Process {
        # Check if any of the views are default
        $DefaultViews = Get-IADefaultView -All

        if($IAView){
            foreach($view in $IAView){
                $GroupsToBeDeleted = Get-IAGroup -All | Where-Object -Property ViewId -eq $view.Id

                foreach($group in $GroupsToBeDeleted) {
                    Remove-IAGroup -Id $group.Id
                }

                foreach($defaultview in $DefaultViews){
                    if($defaultview.ViewId.Contains($view.Id)){
                        Remove-IADefaultView -IADefaultView $defaultview
                    }
                }

                $Uri = "Views($($view.Id))"
                $response = Invoke-IAQuery -QueryUrl $Uri -Method Delete

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

                return $response.value
            }
        }

        if($Id){
            foreach($item in $Id){
                $GroupsToBeDeleted = Get-IAGroup -All | Where-Object -Property ViewId -eq $item

                foreach($group in $GroupsToBeDeleted) {
                    Remove-IAGroup -Id $group.Id
                }
    
                $Uri = "Views($item)"
                $response = Invoke-IAQuery -QueryUrl $Uri -Method Delete

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

                return $response.value
            }
        }
    }
}