Get-MgServicePrincipalCount: Graph PowerShell Cmdlet

Service principals represent applications and services registered in Microsoft Entra ID (Azure AD). As tenants grow, administrators often need a quick, reliable way to know how many service principals exist—either overall or based on specific criteria.

The Get-MgServicePrincipalCount cmdlet provides exactly that by returning a count instead of a full object list, making it fast and efficient for reporting and audits.

🚀 Community Edition Released!

Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.


i) Cmdlet Syntax

Get-MgServicePrincipalCount [-ConsistencyLevel <String> ] [-Filter <String>]

Key parameters:

  • -ConsistencyLevel: Required for advanced queries (for example, when using $filter). Typically set to eventual.
  • -Filter: Narrows down the count based on specific service principal properties.

ii) Usage Examples

Example 1: Executing Get-MgServicePrincipalCount (Passing ConsistencyLevel only when prompted)

Get-MgServicePrincipalCount

Returns the total number of service principals in the tenant.

Example 2: Executing Get-MgServicePrincipalCount by Passing ConsistencyLevel header directly

Get-MgServicePrincipalCount -ConsistencyLevel eventual

Explicitly setting the consistency level to eventual

Example 3: Executing Get-MgServicePrincipalCount with -Filter parameter

Get-MgServicePrincipalCount -ConsistencyLevel eventual ` -Filter "servicePrincipalType eq 'Application'"

Returns the count of service principals where the type is Application (for example, app registrations and enterprise apps backed by applications).


iii) Cmdlet Tips

  • Always use -ConsistencyLevel eventual when working with filters to avoid query failures.
  • This cmdlet returns only a number, not service principal objects—use Get-MgServicePrincipal if you need details.
  • Combine this cmdlet with scripts or scheduled jobs for lightweight inventory tracking.
  • Ideal for large tenants, as counting is faster than enumerating all service principals.

iv) Use Cases

  • Security audits: Quickly assess how many service principals exist in the tenant.
  • Application governance: Track growth in enterprise applications over time.
  • Change monitoring: Compare counts periodically to detect unexpected spikes.
  • Reporting: Include service principal counts in compliance or management dashboards.

v) Possible Errors & Solutions

Error Cause Solution
Request_UnsupportedQuery Missing consistency level Re-run the command with -ConsistencyLevel eventual.
Authorization_RequestDenied Insufficient permissions Ensure you have Application.Read.All or equivalent admin consent.
Count returns 0 unexpectedly Filter is too restrictive Review and validate the filter syntax and property names.
Cmdlet not recognized Microsoft.Graph module missing Install or update the module using Install-Module Microsoft.Graph.


vi) Conclusion

The Get-MgServicePrincipalCount cmdlet is a simple yet powerful tool for administrators who need quick visibility into service principals without retrieving full object details. By combining it with -ConsistencyLevel eventual and optional filters, you can efficiently support audits, governance, and reporting needs across Microsoft Entra ID.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved. Design by HTML Codex