Assign custom roles with resource scope using PowerShell in Azure Active Directory
This article describes how to create a role assignment at organization-wide scope in Azure Active Directory (Azure AD). Assigning a role at organization-wide scope grants access across the Azure AD organization. To create a role assignment with a scope of a single Azure AD resource, see How to create a custom role and assign it at resource scope. This article uses the Azure Active Directory PowerShell Version 2 module.
For more information about Azure AD admin roles, seeĀ Assigning administrator roles in Azure Active Directory.
Required permissions
Connect to your Azure AD organization using a global administrator account to assign or remove roles.
Prepare PowerShell
Install the Azure AD PowerShell module from the PowerShell Gallery. Then import the Azure AD PowerShell preview module, using the following command:
Import-Module AzureADPreview
To verify that the module is ready to use, match the version returned by the following command to the one listed here:
Get-Module AzureADPreview
ModuleType Version Name ExportedCommands
---------- --------- ---- ----------------
Binary 2.0.0.115 azureadpreview {Add-AzureADMSAdministrati...}
Now you can start using the cmdlets in the module. For a full description of the cmdlets in the Azure AD module, see the online reference documentation for Azure AD preview module.
Assign a directory role to a user or service principal with resource scope
- Load the Azure AD PowerShell (Preview) module.
- Sign in by executing the command
Connect-AzureAD
. - Create a new role using the following PowerShell script.
## Assign a role to a user or service principal with resource scope
# Get the user and role definition you want to link
$user = Get-AzureADUser -Filter "userPrincipalName eq 'cburl@f128.info'"
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Application Support Administrator'"
# Get app registration and construct resource scope for assignment.
$appRegistration = Get-AzureADApplication -Filter "displayName eq 'f/128 Filter Photos'"
$resourceScope = '/' + $appRegistration.objectId
# Create a scoped role assignment
$roleAssignment = New-AzureADMSRoleAssignment -ResourceScope $resourceScope -RoleDefinitionId $roleDefinition.Id -PrincipalId $user.objectId
To assign the role to a service principal instead of a user, use the Get-AzureADMSServicePrincipal cmdlet.
Role definitions
Role definition objects contain the definition of the built-in or custom role, along with the permissions that are granted by that role assignment. This resource displays both custom role definitions and built-in directory roles (which are displayed in roleDefinition equivalent form). Today, an Azure AD organization can have a maximum of 30 unique custom role definitions defined.
Create a role definition
# Basic information
$description = "Can manage credentials of application registrations"
$displayName = "Application Registration Credential Administrator"
$templateId = (New-Guid).Guid
# Set of actions to include
$rolePermissions = @{
"allowedResourceActions" = @(
"microsoft.directory/applications/standard/read",
"microsoft.directory/applications/credentials/update"
)
}
# Create new custom directory role
$customAdmin = New-AzureADMSRoleDefinition -RolePermissions $rolePermissions -DisplayName $displayName -Description $description -TemplateId $templateId -IsEnabled $true
Read and list role definitions
# Get all role definitions
Get-AzureADMSRoleDefinitions
# Get single role definition by ID
Get-AzureADMSRoleDefinition -Id 86593cfc-114b-4a15-9954-97c3494ef49b
# Get single role definition by templateId
Get-AzureADMSRoleDefinition -Filter "templateId eq 'c4e39bd9-1100-46d3-8c65-fb160da0071f'"
Update a role definition
# Update role definition
# This works for any writable property on role definition. You can replace display name with other
# valid properties.
Set-AzureADMSRoleDefinition -Id c4e39bd9-1100-46d3-8c65-fb160da0071f -DisplayName "Updated DisplayName"
Delete a role definition
# Delete role definition
Remove-AzureADMSRoleDefinitions -Id c4e39bd9-1100-46d3-8c65-fb160da0071f
Role assignments
Role assignments contain information linking a given security principal (a user or application service principal) to a role definition. If required, you can add a scope of a single Azure AD resource for the assigned permissions. Restricting the scope of a role assignment is supported for built-in and custom roles.
Create a role assignment
# Get the user and role definition you want to link
$user = Get-AzureADUser -Filter "userPrincipalName eq 'cburl@f128.info'"
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Application Support Administrator'"
# Get app registration and construct resource scope for assignment.
$appRegistration = Get-AzureADApplication -Filter "displayName eq 'f/128 Filter Photos'"
$resourceScope = '/' + $appRegistration.objectId
# Create a scoped role assignment
$roleAssignment = New-AzureADMSRoleAssignment -ResourceScope $resourceScope -RoleDefinitionId $roleDefinition.Id -PrincipalId $user.objectId
Read and list role assignments
# Get role assignments for a given principal
Get-AzureADMSRoleAssignment -Filter "principalId eq '27c8ca78-ab1c-40ae-bd1b-eaeebd6f68ac'"
# Get role assignments for a given role definition
Get-AzureADMSRoleAssignment -Filter "roleDefinitionId eq '355aed8a-864b-4e2b-b225-ea95482e7570'"
Delete a role assignment
# Delete role assignment
Remove-AzureADMSRoleAssignment -Id 'qiho4WOb9UKKgng_LbPV7tvKaKRCD61PkJeKMh7Y458-1'
Next steps
- Share with us on the Azure AD administrative roles forum
- For more about roles and Azure AD administrator role assignments, see Assign administrator roles
- For default user permissions, see a comparison of default guest and member user permissions