Ever wished you could bulk invite external users to your SharePoint Online site?
Turns out you can. Here’s the PowerShell.
A couple notes:
- This assumes you have installed the Microsoft SharePoint Online SDK v16 https://www.microsoft.com/en-ca/download/details.aspx?id=42038
-
And that you have added them to your script. Eg:
Add-Type –Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll” Add-Type –Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll” - This script also assumes you have a variable $users which has a collection of email addresses that you want to invite. (Maybe imported from a CSV etc.)
- This sets all users invited to have “view” rights. To change this modify this line accordingly [Microsoft.SharePoint.Client.Sharing.Role]::View
[powershell] # Establish Connection to SharePoint Online $siteUrl = "site url" $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("username", "password") $ctx.Credentials = $credentials # Create request list $userList = New-Object "System.Collections.Generic.List``1[Microsoft.SharePoint.Client.Sharing.UserRoleAssignment]" # For each user, set role # THIS ASSUMES YOU HAVE A POWERSHELL COLLECTION OF USERS YOU WANT TO INVITE ForEach($user in $users) { $userRoleAssignment = New-Object Microsoft.SharePoint.Client.Sharing.UserRoleAssignment $userRoleAssignment.UserId = $user $userRoleAssignment.Role = [Microsoft.SharePoint.Client.Sharing.Role]::View $userList.Add($userRoleAssignment) } try { # Send invites $message = "Please accept this invite to our SharePoint Site. Thanks!" [Microsoft.SharePoint.Client.Sharing.WebSharingManager]::UpdateWebSharingInformation($ctx, $ctx.Web, $userList, $true, $message, $true, $true) $ctx.ExecuteQuery() } catch { $hasError = ($error[0] | out-string) } [/powershell]