Microsoft Entra B2B collaboration code and PowerShell samples

PowerShell example

You can bulk-invite external users to an organization from email addresses that you've stored in a .csv file.

  1. Prepare the .csv file Create a new .csv file and name it invitations.csv. In this example, the file is saved in C:\data, and contains the following information:

    Name InvitedUserEmailAddress
    Gmail B2B Invitee
    Outlook B2B invitee
  2. Get the latest Azure AD PowerShell To use the new cmdlets, you must install the updated Azure AD PowerShell module, which you can download from the PowerShell module's release page

  3. Sign in to your tenancy

    $cred = Get-Credential
    Connect-AzureAD -Credential $cred
  4. Run the PowerShell cmdlet

    $invitations = import-csv C:\data\invitations.csv
    $messageInfo = New-Object Microsoft.Open.MSGraph.Model.InvitedUserMessageInfo
    $messageInfo.customizedMessageBody = "Hey there! Check this out. I created an invitation through PowerShell"
    foreach ($email in $invitations) {New-AzureADMSInvitation -InvitedUserEmailAddress $email.InvitedUserEmailAddress -InvitedUserDisplayName $email.Name -InviteRedirectUrl -InvitedUserMessageInfo $messageInfo -SendInvitationMessage $true}

This cmdlet sends an invitation to the email addresses in invitations.csv. More features of this cmdlet include:

  • Customized text in the email message
  • Including a display name for the invited user
  • Sending messages to CCs or suppressing email messages altogether

Code sample

The code sample illustrates how to call the invitation API and get the redemption URL. Use the redemption URL to send a custom invitation email. You can compose the email with an HTTP client, so you can customize how it looks and send it through the Microsoft Graph API.

Content-type: application/json
  "invitedUserEmailAddress": "",
  "invitedUserDisplayName": "David",
  "inviteRedirectUrl": "",
  "sendInvitationMessage": true

Next steps