Microsoft Entra B2B collaboration code and PowerShell samples

PowerShell example

You can bulk-invite external users to an organization from email addresses you store 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 Microsoft Graph PowerShell

    To use the new cmdlets, you must install the updated Microsoft Graph PowerShell module. For more information, see Install the Microsoft Graph PowerShell SDK

  3. Sign in to your tenancy

    Connect-MgGraph -Scopes "User.Invite.All"
  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-MgInvitation -InviteRedirectUrl "" `
          -InvitedUserDisplayName $email.Name -InvitedUserEmailAddress $email.InvitedUserEmailAddress `
          -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