How to ingest an email list to AD Group as guest

Daniel Hoang Nguyen 0 Reputation points
2024-09-04T07:19:53.0733333+00:00

Hi there,

I have an email list of 800 people, I need to add them as members to my AD Group.

How can I do it automatically, not manually.

User's image

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
10,568 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 23,096 Reputation points
    2024-09-04T12:15:23.26+00:00

    If you haven't already installed the AzureAD module, run this command:

    Install-Module AzureAD
    
    

    You need to authenticate to your Azure AD tenant:

    Connect-AzureAD
    

    Make sure your CSV file is formatted with a header, and the column has the email addresses. For example, guests.csv:

    EmailAddress
    guest1@example.com
    guest2@example.com
    guest3@example.com
    

    Use PowerShell to invite each guest user to Azure AD. Azure AD has a built-in New-AzureADMSInvitation cmdlet for inviting guest users.

    $guestUsers = Import-Csv "C:\path\to\your\guests.csv"
    foreach ($user in $guestUsers) {
        $email = $user.EmailAddress
        New-AzureADMSInvitation -InvitedUserEmailAddress $email -InviteRedirectUrl "https://myapps.microsoft.com" -SendInvitationMessage $true
    }
    

    Once the users have been invited as guests, you can add them to the Azure AD group. First, get the group’s ObjectId:

    $group = Get-AzureADGroup -SearchString "YourGroupName"
    

    Then, loop through the CSV file to add the guest users to the group:

    foreach ($user in $guestUsers) {
        $guest = Get-AzureADUser -Filter "Mail eq '$($user.EmailAddress)'"
        Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId $guest.ObjectId
    }
    

    You can verify that users have been added by listing the group's members:

    Get-AzureADGroupMember -ObjectId $group.ObjectId
    

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.