The New-MgServicePrincipal cmdlet is a powerful tool in the Microsoft Graph PowerShell module enabling administrators to create new service principals within the Microsoft 365 directory. This article delves into the syntax, usage example, tips, use cases, common errors and solutions relating to the cmdlet.
Install-Module Microsoft.Graph -Scope CurrentUser
New-MgServicePrincipal -BodyParameter <Object>
Here’s a straightforward example of creating a new service principal using the cmdlet:
$ServicePrincipalID = @{
"AppId" = "fc876dd1-6bcb-4304-b9b6-18ddf1526b62"
}
New-MgServicePrincipal -BodyParameter $ServicePrincipalID | Format-List id DisplayName AppId SignInAudience
This script creates a service principal with the specified AppId and then formats the output to list key properties.
Expected Output:
Id : ac483a5f-f291-4499-8a62-058547724579
DisplayName : Example App
AppId : ffdf268a-2fe2-49e1-8cd7-66ecb61641ec
SignInAudience : AzureADandPersonalMicrosoftAccount
No, a service principal is always tied to an application registration. You must first register the app (or know its AppId) before creating a corresponding service principal using New-MgServicePrincipal.
AppId
and Id
in the context of service principals?AppId
refers to the unique ID of the application registration across tenants..Id
(or ObjectId
) refers to the unique ID of the service principal object within your specific tenant.When creating a service principal, use AppId
, not ObjectId
New-MgServicePrincipal
manually?You should use it when:
Error | Cause | Solution |
---|---|---|
Insufficient Permissions | Authorization_RequestDenied | Ensure the user has the necessary Azure AD permissions to create service principals. |
Invalid AppId | Request_BadRequest | Verify that the AppId provided in the -BodyParameter is correct and corresponds to a registered application. |
Malformed JSON | Invalid request | Check the JSON structure in the -BodyParameter for syntax errors and ensure all required fields are included. |
If you're registering applications via the Entra admin center or the Graph API, a service principal is typically auto-created in the home tenant. Use New-MgServicePrincipal
only when you need to explicitly register a service principal for multi-tenant apps or external apps not yet represented in your directory.
When creating a new service principal using New-MgServicePrincipal
, always use the -AppId
of the application you want to associate. Using the -AppId
ensures tenant-wide uniqueness, while ObjectId
applies to a single directory object and is not accepted by this cmdlet.
The New-MgServicePrincipal cmdlet is essential for managing application identities within an Azure AD tenant. By understanding its syntax, usage, and handling common errors, M365 administrators can leverage this cmdlet to enhance security, automate tasks, and integrate applications efficiently.
© m365corner.com. All Rights Reserved. Design by HTML Codex