The Microsoft.Xrm.Data.PowerShell module provides a number of very useful functions for getting information from Microsoft CRM/D365 using PowerShell. The Get-CrmEntityRecordCount is just one of those functions but it only returns a count of all records for an entity. What if I need a record count for a filtered view? Fortunately, this is where the Get-CrmRecordsByFetch function can be combined with FetchXml aggregation.
# Create my connection to D365 $conn = Connect-CrmOnlineDiscovery -InteractiveMode # Create my FetchXML with the relevant filter to return an aggregate (Count) $count = Get-CrmRecordsByFetch -conn $conn -Fetch @" <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" aggregate="true"> <entity name="msdyn_workorder"> <attribute name="msdyn_name" alias="msdyn_name_count" aggregate="count" /> <filter type="and"> <condition attribute="createdon" operator="on-or-before" value="2018-02-01" /> </filter> </entity> </fetch> "@ # Get the actual number of records from the AliasedValue $count.CrmRecords[0].msdyn_name_count