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 b2binvitee@gmail.com
    Outlook B2B invitee b2binvitee@outlook.com
  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 https://wingtiptoysonline-dev-ed.my.salesforce.com -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.

POST https://graph.microsoft.com/v1.0/invitations
Content-type: application/json
{
  "invitedUserEmailAddress": "david@fabrikam.com",
  "invitedUserDisplayName": "David",
  "inviteRedirectUrl": "https://myapp.contoso.com",
  "sendInvitationMessage": true
}

Next steps