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 Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

Answer accepted by question author
  1. Tushar Kumar 3,386 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=("******@emraank2gmailcom.onmicrosoft.com" "******@emraank2gmailcom.onmicrosoft.com" "******@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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.