Service principals are security identities that grant applications and services access to Microsoft Entra ID resources, storing key details like application IDs, permissions, roles, and ownership. Admins need to access this information for security, compliance, and access management, during audits, and troubleshooting. While the Graph PowerShell's Get-MgServicePrincipal command, an alternative to the Get-AzureADServicePrincipal command, can be used to retrieve the service principal data, its complexity and non-intuitive interface make it a challenging option.
Before using the Get-MgServicePrincipal cmdlet, ensure the following:
Install-Module Microsoft.Graph -Scope CurrentUser
Application.Read.All, Directory.ReadWrite.All, Directory.Read.All,
Application.ReadWrite.OwnedBy, Application.ReadWrite.All,
The Get-MgServicePrincipal cmdlet can be used in Microsoft Graph PowerShell to retrieve the properties and relationships of a service principal object. Here's the syntax:
Get-MgServicePrincipal
[-ExpandProperty <String[]>]
[-Property <String[]>]
[-Filter <String>]
[-Search <String>]
[-Skip <Int32>]
[-Sort <String[]>]
[-Top <Int32>]
[-ConsistencyLevel <String>]
[-ResponseHeadersVariable <String>]
[-Headers <IDictionary>]
[-PageSize <Int32>]
[-All]
[-CountVariable <String>]
[-ProgressAction <ActionPreference>]
[<CommonParameters>]
Example 1: Get all service principals from the directory
Connect-MgGraph -Scopes 'Application.Read.All'
Get-MgServicePrincipal
Example 2: Get the service principal by display name
Connect-MgGraph -Scopes 'Application.Read.All'
Get-MgServicePrincipal -Filter "DisplayName eq 'Power BI Service'" |
Format-List Id, DisplayName, AppId, SignInAudience
Example 3: Get a count of the service principals
Connect-MgGraph -Scopes 'Application.Read.All'
Get-MgServicePrincipal -ConsistencyLevel eventual -Count spCount
Example 4: Use -Filter and -Top to get five service principals with a display name that starts with "a" including a count of returned objects
Connect-MgGraph -Scopes 'Application.Read.All'
Get-MgServicePrincipal -ConsistencyLevel eventual -Count spCount -Filter "startsWith(DisplayName, 'a')" -Top 5
Example 5: Use -Search to get service principals with display names that contain the letters "Team" including a count of returned objects
Connect-MgGraph -Scopes 'Application.Read.All'
Get-MgServicePrincipal -ConsistencyLevel eventual -Count spCount -Search '"DisplayName:Team"'
The following table contains some parameters that can be used along with the Get-MgServicePrincipal command.
| Parameters | Description |
|---|---|
| -CountVariable | This parameter specifies a count of the total number of items in a collection. |
| -Filter | This parameter filters items by property values. |
| -Skip | This parameter skips the first n items. |
| -Sort | This parameter sorts items by property values. |
| -Top | This parameter displays only the first n items. |
ManageEngine ADManager Plus is an identity governance and administration solution, with comprehensive Microsoft Entra ID management and reporting capabilities, that eliminates the hassle of complex PowerShell scripts. Its intuitive, user-friendly interface enables you to streamline even the most complex administrative tasks in minutes.