How do assign multiple users to different groups in Azure via Azure cli/Bash

Emran Hossain 195 Reputation points
2023-09-20T13:24:53.99+00:00

Hi ,

I am facing some problem to do a task regarding "How do assign multiple users to different groups in Azure via Azure cli/Bash".

I am not working too much on Azure Cli.

I have a 4 Group & 10 users to assign different groups by Azure cli via create script will be the best .

But i have no idea where to start .

Someone has a good idea how to fix it, will be really appreciated .

BR,

Emmi

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,833 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tushar Kumar 3,326 Reputation points MVP
    2023-09-20T13:51:31.9966667+00:00
    group_name="YourGroupName"  # Replace with the name of the Azure AD group
    
    # Define an array of UPNs of users to add to the group
    user_upns=("user1@example.com" "user2@example.com" "user3@example.com")
    
    # Loop through the array and add each user to the Azure AD group
    for upn in "${user_upns[@]}"; do
      # Get the object ID of the user
      user_object_id=$(az ad user show --upn "$upn" --query objectId --output tsv)
     
      # Add the user to the Azure AD group
      az ad group member add --group "$group_name" --members "$user_object_id"
    done
    

    You can use the above script to add multiple users to a group, You can use the same priniciple to tweak it for your requirements

    Please click "Accept as answer" if it helps.


1 additional answer

Sort by: Most helpful
  1. Emran Hossain 195 Reputation points
    2023-09-21T09:28:12.3166667+00:00
    
    
    group_name="ManagementGroupB"
    
     
    
    user_upns=("testmgm01@emraank2gmailcom.onmicrosoft.com" "testmgm02@emraank2gmailcom.onmicrosoft.com" "testmgm03@emraank2gmailcom.onmicrosoft.com")
    
     
    
    for upn in "${user_upns[@]}"; do
    
        user_object_id=$(az ad user show --upn "$upn" --query objectId --output tsv)
    
        az ad group member add --group "$group_name" --members "$user_object_id"
    
    done
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.