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.

Frequently Asked Questions

  • What happens if I invite a guest user with the wrong email address?
  • If the email is invalid, the invitation will fail to send. If it’s a typo in a valid domain, the invitation may go to the wrong person. Always use -WhatIf to test before bulk invites.

  • Can I resend an invitation if the guest hasn’t accepted it?
  • Yes. You can re-run New-MgInvitation with the same email address and add the -SendInvitationMessage parameter to resend the invite.

  • How do I check if a guest has already accepted the invitation?
  • You can run Get-MgUser -Filter "userType eq 'Guest'" to list guest accounts. Accepted invitations will appear here with a valid UserPrincipalName.

  • Is it possible to customize the invitation email message?
  • Yes. The -InvitedUserMessageInfo parameter lets you add a custom subject and body text, making the invite more personalized and informative.


Possible Errors You Might Face

Error Cause Solution
Insufficient Permissions Insufficient privileges to complete the operation. Ensure you have the necessary permissions. Connect to Microsoft Graph with the appropriate scopes: Connect-MgGraph -Scopes "User.Invite.All"
Invalid Redirect URL The redirect URL is not valid. 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 Invalid email address. Ensure the email addresses in your CSV file or script are correctly formatted and valid.
CSV File Issues Cannot process argument because the value of argument "path" is not valid. 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 Too many requests. 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.


💡 Include -WhatIf for Safe Bulk Invites

When inviting multiple guest users—particularly in bulk scripts—use the -WhatIf parameter with New-MgInvitation to simulate the operation first. This helps prevent accidental invitations to emails with typos or invalid addresses.
💡 Handle Redeemed Invitations with -ResetRedemption

If a guest user has already accepted a previous invitation and you'd like to resend or reset the redemption link, use the -ResetRedemption flag. This reactivates the invitation without creating duplicate guest accounts.

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