How to Add Members to Entra Groups Using PowerShell

Managing group memberships within Microsoft Entra ID (formerly Azure Active Directory) is a critical task for IT administrators. Whether it’s for setting access permissions or streamlining user roles across services, knowing how to automate these processes can save hours of manual work. One such method is utilizing PowerShell to add members to Entra groups efficiently. In this article, we’ll walk you through the steps to get started and offer best practices that will elevate your scripting game.

Why Use PowerShell for Managing Entra Groups?

While the Azure portal provides a graphical interface for managing Entra ID, it’s not always scalable or efficient—especially when dealing with a large number of users. PowerShell offers:

  • Automation: Execute tasks programmatically, reducing human error.
  • Scalability: Manage thousands of users with a single script.
  • Speed: Perform bulk operations in seconds.
  • Flexibility: Integrate with other scripts and administrative workflows.

The combination of speed and precision makes PowerShell an indispensable tool for systems administrators.

Setting Up Your Environment

Before you can start adding members using PowerShell, you need to make sure your local environment is configured correctly.

  1. Install the AzureAD or Microsoft Graph module:

    For newer scripts, Microsoft Graph is recommended over the AzureAD module due to broader support and future compatibility.

    Install-Module Microsoft.Graph -Scope CurrentUser
  2. Connect to Microsoft Graph:
    Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.Read.All"
  3. Select the correct profile (if needed):
    Select-MgProfile -Name "beta"

Once these steps are complete, you’re ready to query and manage group memberships via script.

Adding a Single Member to an Entra Group

If you have just one user to add, the process is straightforward. You’ll need two pieces of information:

  • The Group ID – You can retrieve this using Get-MgGroup.
  • The User ID – Obtain this with Get-MgUser based on userPrincipalName or other attributes.

# Example: Add one user to a group
$groupId = (Get-MgGroup -Filter "displayName eq 'Sales Team'").Id
$userId = (Get-MgUser -Filter "userPrincipalName eq 'jane.doe@domain.com'").Id

New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId

After running this command successfully, the user will be added to the target Entra group. Simple as that!

Bulk Adding Users from a CSV File

When scaling these operations, PowerShell really shines. Here’s how to add multiple users from a CSV file:


Import-Csv "C:\UsersToAdd.csv" | ForEach-Object {
    $userId = (Get-MgUser -Filter "userPrincipalName eq '$($_.UserPrincipalName)'").Id
    New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
}

Make sure your CSV has a column named UserPrincipalName. This method lets you manage group memberships in bulk intelligently and repeatably.

Verifying Group Membership

After executing your script, it’s essential to confirm that users were added successfully. Use the following command to list all members of a group:


Get-MgGroupMember -GroupId $groupId | Select-Object Id,DisplayName,UserPrincipalName

Alternatively, use the Azure Portal for a graphical view of group members.

Troubleshooting Common Errors

Even seasoned admins sometimes stumble upon errors. Here are a few common ones to watch for:

  • Access Denied: Make sure your account has the necessary Graph API permissions.
  • Null ID Values: Check that user and group filters return valid results.
  • Throttling: Use throttling mitigation strategies like delay loops for large-scale scripts.

Always test scripts on smaller user groups before deploying them organization-wide.

Conclusion

Using PowerShell to manage Entra ID group memberships combines the power of automation with the flexibility needed in modern IT environments. With just a few commands, you can add individual users or handle bulk operations seamlessly. As your directory grows, mastering these tools ensures efficient and error-free management across your digital infrastructure. Don’t just manage—automate, optimize, and take control with PowerShell and Microsoft Graph.