Assign an Entra ID Directory Role to a User via Microsoft Graph

Prev Next

Grant administrative privileges by assigning a built-in directory role such as Cloud Application Administrator to a user account.

Complexity: Intermediate. Impact: security + access-control + privileged-access + identity.

Why use this workflow

Directory roles in Microsoft Entra ID grant users administrative privileges over specific parts of your tenant, from managing applications to resetting passwords to full Global Administrator control. Assigning the right role (and no more) is the cornerstone of least-privilege administration.

This workflow walks through assigning a built-in directory role to a user with Microsoft Graph: find the user, locate the role definition, check for existing assignments, and make the assignment.

When to run this

  • You onboard a new admin or delegate specific administrative duties
  • You grant time-limited elevated access for a project (consider PIM for this)
  • You automate role provisioning as part of a broader identity lifecycle workflow
  • You replace ad-hoc Global Administrator grants with scoped roles (for example, Cloud Application Administrator)

Expected outcomes

  • The target user holds the specified directory role, scoped directory-wide (/) by default
  • A unifiedRoleAssignment object in Entra ID records the assignment (visible in the audit log)
  • The new permissions take effect after propagation. The user may need to sign out and back in
  • The assignment is visible under Entra admin center > Roles and administrators > the role > Assignments

Risks and considerations

  • Least privilege first. Do not reach for Global Administrator. Built-in roles such as Cloud Application Administrator, User Administrator, or Helpdesk Administrator cover most delegation needs.
  • Permanent versus eligible. The roleAssignments endpoint creates a permanent (active) assignment. For sensitive roles, use Privileged Identity Management (PIM) to create eligible assignments that require just-in-time activation, MFA, and approval.
  • MFA enforcement. Make sure that the target user has a strong authentication method registered before they use their new privileges. Your tenant may enforce MFA for admin portals.
  • Audit everything. Role assignments are high-signal events. Confirm that your tenant sends these events to a SIEM and alerts on unexpected changes.
  • Do not share admin accounts, assign roles to service accounts without a clear justification, or leave standing Global Administrator assignments for human users.

Required permissions

PermissionWhy it's needed
RoleManagement.ReadWrite.DirectoryRequired to create role assignments through the roleManagement/directory endpoint
User.Read.AllRequired to look up the target user by display name or UPN
Directory.Read.AllRequired to enumerate role definitions and existing assignments

Ask Dex

Paste the prompt into a Dex conversation.

Prompt for IT admins

Assign the {role_name} directory role to {user_upn}. Verify they don't already hold it, create an active assignment at tenant scope, and confirm the assignment was recorded.

Manual steps (admin center)

  1. Sign in to the Microsoft Entra admin center as a Privileged Role Administrator or Global Administrator.
  2. Go to Identity > Roles & admins > Roles & admins.
  3. In the roles list, search for the role that you want to assign (for example, Cloud Application Administrator) and select its name.
  4. Select Add assignments at the top of the role page.
  5. Select No member selected, then search for and select the target user (for example, {user_upn}). Select Select.
  6. On the Setting tab, choose the assignment type:
    • Active: a standing assignment, with privileges always available
    • Eligible (PIM P2 required): the user must activate the role just in time
  7. Optionally, set a start and end date for time-bound assignments and add a justification.
  8. Select Assign.
  9. Verify that the assignment appears under the role's Assignments, and confirm that the change appears under Identity > Monitoring & health > Audit logs.

Verification

Ask the user to sign out and back in, then confirm that they can access the role's scope. For example, for Cloud Application Administrator, they should be able to open App registrations and create a new app.

Microsoft Graph API

The workflow has four API calls: find the user, find the role definition, check for an existing assignment, and create the assignment.

1. Find the target user

GET https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'{name}') or startswith(userPrincipalName,'{name}')&$select=id,displayName,userPrincipalName

Response:

{
  "value": [{
    "id": "02246633-8196-4257-a8c9-657b77773a8e",
    "displayName": "Alex Example",
    "userPrincipalName": "AlexE@contoso.onmicrosoft.com"
  }]
}

2. Find the role definition

GET https://graph.microsoft.com/v1.0/roleManagement/directory/roleDefinitions?$filter=displayName eq '{role_name}'

For well-known built-in roles, you can skip this step and use the template ID directly. Example IDs:

  • 158c047a-c907-4556-b7ef-446551a6b5f7: Cloud Application Administrator
  • fe930be7-5e62-47db-91af-98c3a49a38b1: User Administrator
  • 729827e3-9c14-49f7-bb1b-9608f156bbb8: Helpdesk Administrator
  • 62e90394-69f5-4237-9190-012177145e10: Global Administrator (use with extreme caution)

3. Check for an existing assignment (idempotency)

GET https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments?$filter=roleDefinitionId eq '{role_id}' and principalId eq '{user_id}'

4. Create the role assignment

POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments
Content-Type: application/json

{
  "principalId": "{user_id}",
  "roleDefinitionId": "{role_id}",
  "directoryScopeId": "/"
}

A successful response is HTTP 201 Created with the new assignment object:

{
  "id": "egSMFQfJVkW370RlUaa19zNmJAKWgVdCqMlle3d3Oo4-1",
  "principalId": "02246633-8196-4257-a8c9-657b77773a8e",
  "directoryScopeId": "/",
  "roleDefinitionId": "158c047a-c907-4556-b7ef-446551a6b5f7"
}

Scope options

directoryScopeId controls where the role applies:

  • /: tenant-wide (most built-in roles)
  • /administrativeUnits/{auId}: scoped to an administrative unit
  • /{appObjectId}: scoped to a single application (for app-scoped role assignments)

PowerShell

The Microsoft.Graph PowerShell SDK wraps the Graph endpoints shown above. Install it once:

Install-Module Microsoft.Graph -Scope CurrentUser

Interactive quickstart

# Connect with the required scopes
Connect-MgGraph -Scopes "RoleManagement.ReadWrite.Directory","User.Read.All","Directory.Read.All"

# 1. Find the user
$user = Get-MgUser -UserId "user@contoso.com"

# 2. Find the role definition
$role = Get-MgRoleManagementDirectoryRoleDefinition -Filter "displayName eq 'Cloud Application Administrator'"

# 3. Create the assignment
New-MgRoleManagementDirectoryRoleAssignment -PrincipalId $user.Id -RoleDefinitionId $role.Id -DirectoryScopeId "/"

Walkthrough

  1. Authenticate. Connect-MgGraph prompts for interactive sign-in with delegated scopes. For unattended use, authenticate with a client secret or certificate against an app registration.
  2. Resolve identities. Pass the UPN (user@contoso.com) rather than a display name. It is unique and avoids ambiguity.
  3. Idempotency. Check for an existing assignment before you create a new one, so that the script is safe to re-run.
  4. Confirm. Use Get-MgRoleManagementDirectoryRoleAssignment -Filter "principalId eq '$($user.Id)'" to list all active role assignments for the user.