The New-MgRoleManagementDirectoryRoleAssignment cmdlet creates a new directory role assignment in Microsoft Entra ID (previously Azure AD). This allows you to programmatically assign administrative roles to users or service principals.
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "RoleManagement.ReadWrite.Directory"
Run the Graph PowerShell command below to create a new role assignment. Replace <role-id> with the role you want to assign, and <user-or-sp-id> with the user or service principal ID:
New-MgRoleManagementDirectoryRoleAssignment -DirectoryRoleId <role-id> -PrincipalId <user-or-sp-id>
This Graph PowerShell command assigns the Global Administrator role to a user:
New-MgRoleManagementDirectoryRoleAssignment -DirectoryRoleId "role-id" -PrincipalId "user-id"
This Graph PowerShell command assigns a custom administrative role to a service principal:
New-MgRoleManagementDirectoryRoleAssignment -DirectoryRoleId "custom-role-id" -PrincipalId "sp-id"
This Graph PowerShell command assigns a role and includes a description for the assignment:
New-MgRoleManagementDirectoryRoleAssignment -DirectoryRoleId "role-id" -PrincipalId "user-id" -Description "Temporary assignment for project"
The following are some essential parameters that can be used along with the New-MgRoleManagementDirectoryRoleAssignment command:
| Parameters | Description |
|---|---|
| -DirectoryRoleId | The role to be assigned |
| -PrincipalId | The user or service principal being assigned the role |
| -Description | (Optional) Custom description for the assignment |
| -ResourceScopeId | (Optional) Scope of the assignment, if applicable |
| -WhatIf | Shows what would happen if the cmdlet ran, without executing it |
ADManager Plus, an identity governance and administration solution with comprehensive Microsoft Entra ID management and reporting capabilities, simplifies complex admin tasks from a single, user-friendly console: