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.
Install-Module Microsoft.Graph -Scope CurrentUser
.Connect-MgGraph -Scopes "User.Invite.All"
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"
}
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"
}
}
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"
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.
Error: Invalid email address.
Solution: Ensure the email addresses in your CSV file or script are correctly formatted and valid.
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.
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.
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex