Muokkaa

Jaa


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

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