How to Invite Guest Users to Microsoft 365 Using Graph PowerShell

Inviting guest users to Microsoft 365 allows external users to collaborate with your organization. This guide will show you how to invite guest users individually and in bulk using Graph PowerShell.


Prerequisites

  • Install the Microsoft Graph PowerShell module by running Install-Module Microsoft.Graph -Scope CurrentUser.
  • Connect to Microsoft Graph with the necessary permissions:
    Connect-MgGraph -Scopes "User.Invite.All"

Inviting a Single Guest User

To invite a single guest user and send an email invitation, use the following script:

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Invite.All"

# Define the guest user's details
$guestUserEmail = "guestuser@example.com"           # Replace with the guest user's email address
$invitedUserDisplayName = "Guest User"              # Replace with the guest user's display name
$invitedUserMessage = "You have been invited to join our organization. Please click the link to accept the invitation."  # Customize this message as needed

# Create the invitation
$invitation = New-MgInvitation -InvitedUserEmailAddress $guestUserEmail -InvitedUserDisplayName $invitedUserDisplayName -InviteRedirectUrl "https://portal.office.com" -SendInvitationMessage -InvitedUserMessageInfo @{customizedMessageBody = $invitedUserMessage}

# Output the result
if ($invitation.Status -eq "PendingAcceptance") {
    Write-Output "Invitation sent successfully to $guestUserEmail"
} else {
    Write-Output "Failed to send invitation to $guestUserEmail"
}

Inviting Multiple Guest Users from a CSV File

For inviting multiple guest users, prepare a CSV file (guestUsers.csv) with the following format:

Email,DisplayName,Message
guestuser1@example.com,Guest User One,Welcome to our organization!
guestuser2@example.com,Guest User Two,Please join us for an exciting project!

Then use this script to send the invitations:

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Invite.All"

# Path to the CSV file
$csvPath = "path_to_your_csv_file.csv"

# Import the CSV file
$guestUsers = Import-Csv -Path $csvPath

foreach ($guestUser in $guestUsers) {
    # Read guest user details from CSV
    $guestUserEmail = $guestUser.Email
    $invitedUserDisplayName = $guestUser.DisplayName
    $invitedUserMessage = $guestUser.Message

    # Create the invitation
    $invitation = New-MgInvitation -InvitedUserEmailAddress $guestUserEmail -InvitedUserDisplayName $invitedUserDisplayName -InviteRedirectUrl "https://portal.office.com" -SendInvitationMessage -InvitedUserMessageInfo @{customizedMessageBody = $invitedUserMessage}

    # Output the result
    if ($invitation.Status -eq "PendingAcceptance") {
        Write-Output "Invitation sent successfully to $guestUserEmail"
    } else {
        Write-Output "Failed to send invitation to $guestUserEmail"
    }
}

Inviting Guest Users Using Microsoft 365 Admin Center

  1. Select Users >> Guest Users option >> Select Add a guest user
  2. Microsoft Entra Admin Center opens up >> Select Invite User >> Fill in the user personal details >> Click Invite button.

Possible Errors You Might Face

  • Insufficient Permissions
    Error: Insufficient privileges to complete the operation.
    Solution: Ensure you have the necessary permissions. Connect to Microsoft Graph with the appropriate scopes:
    Connect-MgGraph -Scopes "User.Invite.All"
  • Invalid Redirect URL
    Error: The redirect URL is not valid.
    Solution: Verify that the InviteRedirectUrl is a valid URL. Use commonly accepted URLs like https://portal.office.com or your organization's specific URL.
  • Invalid Email Address
    Error: Invalid email address.
    Solution: Ensure the email addresses in your CSV file or script are correctly formatted and valid.
  • CSV File Issues
    Error: Cannot process argument because the value of argument "path" is not valid.
    Solution: Check the path to the CSV file and ensure it is correct. Also ensure the CSV file has the correct headers: Email, DisplayName, Message.
  • Rate Limiting
    Error: Too many requests.
    Solution: If you are inviting a large number of guests, you might hit the rate limit. Consider adding delays between requests or running the script in smaller batches.

Conclusion

Using these simple scripts, you can efficiently manage guest user invitations in Microsoft 365, enhancing collaboration with external users. Whether you need to invite a single user or multiple users in bulk, Graph PowerShell provides a powerful and flexible solution.

For more PowerShell scripts and tips on managing Microsoft 365, visit M365Corner.com.


Suggested Reading:

How to Monitor Guest User Invitations in Microsoft 365 Using Graph PowerShell
Checking Guest User Sign-In Activity with Graph PowerShell
Microsoft 365 Guest User Account Reporting Using Graph PowerShell
Disabling Microsoft 365 Guest User Accounts Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex