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