Tutorial: Grant a group access to Azure resources using Azure PowerShell

Azure role-based access control (Azure RBAC) is the way that you manage access to Azure resources. In this tutorial, you grant a group access to view everything in a subscription and manage everything in a resource group using Azure PowerShell.

In this tutorial, you learn how to:

  • Grant access for a group at different scopes
  • List access
  • Remove access

If you don't have an Azure subscription, create a Trial before you begin.

Note

We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

Prerequisites

To complete this tutorial, you will need:

Role assignments

In Azure RBAC, to grant access, you create a role assignment. A role assignment consists of three elements: security principal, role definition, and scope. Here are the two role assignments you will perform in this tutorial:

Security principal Role definition Scope
Group
(RBAC Tutorial Group)
Reader Subscription
Group
(RBAC Tutorial Group)
Contributor Resource group
(rbac-tutorial-resource-group)

Role assignments for a group

Create a group

To assign a role, you need a user, group, or service principal. If you don't already have a group, you can create one.

  • In Azure powershell, create a new group using the New-MgGroup command.

    New-MgGroup -DisplayName "RBAC Tutorial Group" -MailEnabled:$false `
        -SecurityEnabled:$true -MailNickName "NotSet"
    
    DisplayName         Id                                   MailNickname Description GroupTypes
    -----------         --                                   ------------ ----------- ----------
    RBAC Tutorial Group aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb NotSet                   {}
    

If you don't have permissions to create groups, you can try the Tutorial: Grant a user access to Azure resources using Azure PowerShell instead.

Create a resource group

You use a resource group to show how to assign a role at a resource group scope.

  1. Get a list of region locations using the Get-AzLocation command.

    Get-AzLocation | select Location
    
  2. Select a location near you and assign it to a variable.

    $location = "chinanorth"
    
  3. Create a new resource group using the New-AzResourceGroup command.

    New-AzResourceGroup -Name "rbac-tutorial-resource-group" -Location $location
    
    ResourceGroupName : rbac-tutorial-resource-group
    Location          : chinanorth
    ProvisioningState : Succeeded
    Tags              :
    ResourceId        : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group
    

Grant access

To grant access for the group, you use the New-AzRoleAssignment command to assign a role. You must specify the security principal, role definition, and scope.

  1. Get the object ID of the group using the Get-MgGroup command.

    Get-MgGroup -Filter "DisplayName eq 'RBAC Tutorial Group'"
    
    DisplayName         Id                                   MailNickname Description GroupTypes
    -----------         --                                   ------------ ----------- ----------
    RBAC Tutorial Group aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb NotSet                   {}
    
  2. Save the group object ID in a variable.

    $groupId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
    
  3. Get the ID of your subscription using the Get-AzSubscription command.

    Get-AzSubscription
    
    Name     : Pay-As-You-Go
    Id       : 00000000-0000-0000-0000-000000000000
    TenantId : aaaabbbb-0000-cccc-1111-dddd2222eeee
    State    : Enabled
    
  4. Save the subscription scope in a variable.

    $subScope = "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e"
    
  5. Assign the Reader role to the group at the subscription scope.

    New-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Reader" `
      -Scope $subScope
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    
  6. Assign the Contributor role to the group at the resource group scope.

    New-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Contributor" `
      -ResourceGroupName "rbac-tutorial-resource-group"
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Contributor
    RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    

List access

  1. To verify the access for the subscription, use the Get-AzRoleAssignment command to list the role assignments.

    Get-AzRoleAssignment -ObjectId $groupId -Scope $subScope
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleAssignments/ffffffff-eeee-dddd-cccc-bbbbbbbbbbb0
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    

    In the output, you can see that the Reader role has been assigned to the RBAC Tutorial Group at the subscription scope.

  2. To verify the access for the resource group, use the Get-AzRoleAssignment command to list the role assignments.

    Get-AzRoleAssignment -ObjectId $groupId -ResourceGroupName "rbac-tutorial-resource-group"
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rbac-tutorial-resource-group
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Contributor
    RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    
    RoleAssignmentId   : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleAssignments/ffffffff-eeee-dddd-cccc-bbbbbbbbbbb0
    Scope              : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e
    DisplayName        : RBAC Tutorial Group
    SignInName         :
    RoleDefinitionName : Reader
    RoleDefinitionId   : acdd72a7-3385-48ef-bd42-f606fba81ae7
    ObjectId           : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    ObjectType         : Group
    CanDelegate        : False
    

    In the output, you can see that both the Contributor and Reader roles have been assigned to the RBAC Tutorial Group. The Contributor role is at the rbac-tutorial-resource-group scope and the Reader role is inherited at the subscription scope.

(Optional) List access using the Azure Portal

  1. To see how the role assignments look in the Azure portal, view the Access control (IAM) blade for the subscription.

    Role assignments for a group at subscription scope

  2. View the Access control (IAM) blade for the resource group.

    Role assignments for a group at resource group scope

Remove access

To remove access for users, groups, and applications, use Remove-AzRoleAssignment to remove a role assignment.

  1. Use the following command to remove the Contributor role assignment for the group at the resource group scope.

    Remove-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Contributor" `
      -ResourceGroupName "rbac-tutorial-resource-group"
    
  2. Use the following command to remove the Reader role assignment for the group at the subscription scope.

    Remove-AzRoleAssignment -ObjectId $groupId `
      -RoleDefinitionName "Reader" `
      -Scope $subScope
    

Clean up resources

To clean up the resources created by this tutorial, delete the resource group and the group.

  1. Delete the resource group using the Remove-AzResourceGroup command.

    Remove-AzResourceGroup -Name "rbac-tutorial-resource-group"
    
    Confirm
    Are you sure you want to remove resource group 'rbac-tutorial-resource-group'
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
    
  2. When asked to confirm, type Y. It will take a few seconds to delete.

  3. Delete the group using the Remove-MgGroup command.

    Remove-MgGroup -GroupID $groupId
    

    If you receive an error when you try to delete the group, you can also delete the group in the portal.

Next steps