Functions/Query/Get-CdsTotalRecordCount.ps1

<#
    .SYNOPSIS
    Returns total number of rows in given entity / table
#>

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

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]   
        [String[]]
        $LogicalNames
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);       
    }    
    process {
       
        $retrieveTotalRecordCountRequest = New-CdsRequest -Name "RetrieveTotalRecordCount";
        $retrieveTotalRecordCountRequest | Add-CdsRequestParameter -Name "EntityNames" -Value $LogicalNames | Out-Null;

        $response = $CdsClient | Invoke-CdsRequest -Request $retrieveTotalRecordCountRequest;
        $response.Results["EntityRecordCountCollection"];
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Get-CdsTotalRecordCount -Alias *;


Register-ArgumentCompleter -CommandName Get-CdsTotalRecordCount -ParameterName "LogicalNames" -ScriptBlock {

    param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)

    $validLogicalNames = Get-CdsEntitiesLogicalName;
    return $validLogicalNames | Where-Object { $_ -like "$wordToComplete*"};
}