Functions/Plugins/Remove-CdsPluginsFromAssembly.ps1

<#
    .SYNOPSIS
    Remove Plugins Steps and Types From Assembly
#>

function Remove-CdsPluginsFromAssembly {
    [CmdletBinding()]    
    param
    (        
        [Parameter(Mandatory=$false, ValueFromPipeline)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]
        $CdsClient = $Global:CdsClient,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]
        $AssemblyName = "Plugins"
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);       
    }    
    process {
        $assembly = Get-CdsRecord -LogicalName "pluginassembly" -AttributeName "name" -Value $AssemblyName;
        if(-not $assembly)
        {
            return;
        }

        $querySteps = New-CdsQueryExpression -LogicalName "sdkmessageprocessingstep";
        $link = $querySteps | Add-CdsQueryLink -ToEntityName "plugintype" -FromAttributeName "eventhandler" -ToAttributeName "plugintypeid"  `
                            | Add-CdsQueryLinkCondition -Field "pluginassemblyid" -Condition Equal -Values $assembly.Id;
        $steps = $CdsClient | Get-CdsMultipleRecords -Query $querySteps;

        ForEach-ObjectWithProgress -Collection $steps -OperationName "Removing plugin steps from $AssemblyName" -ScriptBlock {
            param($step)

            Remove-CdsRecord -CdsClient $CdsClient -Record $step.Record;
        }

        $queryTypes = New-CdsQueryExpression -LogicalName "plugintype" `
                    | Add-CdsQueryCondition -Field "pluginassemblyid" -Condition Equal -Values $assembly.Id `
                    | Add-CdsQueryCondition -Field "isworkflowactivity" -Condition Equal -Values $false;
        $types = $CdsClient | Get-CdsMultipleRecords -Query $queryTypes;

        ForEach-ObjectWithProgress -Collection $types -OperationName "Removing plugin types from $AssemblyName" -ScriptBlock {
            param($type)

            Remove-CdsRecord -CdsClient $CdsClient -Record $type.Record;
        }
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Remove-CdsPluginsFromAssembly -Alias *;