The Get-MgServicePrincipal cmdlet in Microsoft Graph PowerShell allows administrators to retrieve and manage service principals within their Microsoft 365 tenant. Service principals represent applications and services registered in Entra ID (Azure AD), and this cmdlet provides flexible options to filter, search, and display these entities effectively.
Get-MgServicePrincipal [-All] [-ConsistencyLevel <String>] [-CountVariable <String>] [-Filter <String>] [-Search <String>]
Get-MgServicePrincipal
This command retrieves all service principals available in the tenant.
Get-MgServicePrincipal -Filter "DisplayName eq 'Power BI Service'" | Format-List Id, DisplayName, AppId, SignInAudience
This retrieves the service principal for Power BI Service and displays selected properties.
Get-MgServicePrincipal -ConsistencyLevel eventual -Count spCount -Filter "startsWith(DisplayName, 'a')" -Top 5
This command fetches the top 5 service principals whose display names start with 'a'.
Get-MgServicePrincipal -ConsistencyLevel eventual -Count spCount -Search '"DisplayName:Team"'
This searches for service principals with 'Team' in their display names.
This is one of the most common real-world scenarios โ identifying the Enterprise Application using its Application (Client) ID.
Get-MgServicePrincipal `
-Filter "appId eq '00000003-0000-0000-c000-000000000000'" `
-Property Id,DisplayName,AppId |
Select-Object Id, DisplayName, AppId
Useful when: i) Troubleshooting OAuth issues, ii) Identifying Microsoft-built apps (e.g., Microsoft Graph), iii) Mapping App Registration โ Enterprise Application and iv) Uses server-side filtering (efficient and scalable)
This is helpful for auditing newly added Enterprise Applications.
Get-MgServicePrincipal `
-Filter "createdDateTime ge $Date" `
-Property Id,DisplayName,AppId,CreatedDateTime |
Select-Object DisplayName, AppId, CreatedDateTime
| Error Message | Cause | Solution |
| Request_UnsupportedQuery | Unsupported query syntax in - Filter or -Search. | Ensure the correct property names and supported query operators. |
| InvalidAuthenticationToken | Expired or invalid authentication token. | Re-authenticate using Connect-MgGraph. |
| ResourceNotFound | The service principal does not exist. | Verify the display name or AppId |
PublisherNameAppId to Match Against Enterprise ApplicationsAppId from sign-in logs with the AppId of service principals.The Get-MgServicePrincipal cmdlet is essential for managing service principals in Microsoft 365. By mastering its usage, you can efficiently audit, filter, and retrieve service principal details for your tenant. Always ensure proper permissions and use -ConsistencyLevel eventual for advanced filtering and searching.
The /servicePrincipals Graph API endpoint allows you to list, filter, and search Azure AD service principal objects. Below are examples that replicate common Get-MgServicePrincipal use cases using direct API calls.
$uri = "https://graph.microsoft.com/v1.0/servicePrincipals"
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
foreach ($sp in $response.value) {
Write-Output "Display Name: $($sp.displayName)"
Write-Output "App ID : $($sp.appId)"
Write-Output "Object ID : $($sp.id)"
Write-Output "`n"
}
โ
Equivalent to: Get-MgServicePrincipal
Youโll need one of the following Microsoft Graph permissions:
Directory.Read.AllDirectory.Read.All© Created and Maintained by LEARNIT WELL SOLUTIONS. All Rights Reserved.