SharePoint Online helps organizations improve collaboration by sharing documents with both internal and external users. In this blog, we are going to explore how to modify Site sharing settings in a SharePoint Online site using PnP PowerShell.
Summary
- Update Sharing settings from SharePoint Online site interface
- Enable Sharing feature with PnP PowerShell to share files, folders, and sites
- Enable Sharing feature with PnP PowerShell to share files and folders
- Disable Sharing feature for Non-Owners with PnP PowerShell
Update Sharing settings from SharePoint Online site interface
Follow the below steps to update the Sharing settings from the SharePoint Online site interface.
- Navigate to the site collection root, click the Gear Icon (Settings icon) in the top-right, and select Site permissions.
- On the Permissions page, under Site Sharing, click Change how members can share. Now, you can select the required Sharing permission option and save the changes.
There is no direct option to modify Sharing permission settings using SharePoint Online Management PowerShell. Administrators can use the Set-SPOSite cmdlet to disable sharing permissions for non-owners (site members and guest users) with the DisableSharingForNonOwners switch. However, the Set-SPOSite cmdlet doesn’t allow us to enable the sharing feature for non-owners.
We can use the PnP PowerShell to update the sharing permissions for owners and members. Use the following examples to update the three site sharing permission settings.
Enable Sharing feature for owners and members to share files, folders, and sites
The following PnP PowerShell script helps you to enable the sharing permission for the owners and members in the given SharePoint Online site. This will enable the first option “Site owners and members can share files, folders, and the site. People with Edit permissions can share files and folders”. This is usually the default setting assigned to new sites.
#Provide SPO Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/sitename"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
$Web = Get-PnPWeb
$Context = Get-PnPContext
#Update Sharing permission setting
#Set first option - Site owners and members can share files, folders, and the site. People with Edit permissions can share files and folders. This is usually the default setting assigned to new sites.
$Web.MembersCanShare = $true
$Web.Update()
$Web.Context.ExecuteQuery()
#Set "True" for AssociatedMemberGroup.AllowMembersEditMembership
$AssociatedMember = $web.AssociatedMemberGroup
$Context.Load($AssociatedMember)
$Context.ExecuteQuery()
$Web.AssociatedMemberGroup.AllowMembersEditMembership = $true
$Web.AssociatedMemberGroup.Update();
$Web.Context.ExecuteQuery()
Enable Sharing feature for owners and members to share files and folders
This script helps you to enable the second sharing permission option “Site owners and members, and people with Edit permissions can share files and folders, but only site owners can share the site”.
#Provide SPO Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/sitename"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
$Web = Get-PnPWeb
$Context = Get-PnPContext
#Update Sharing permission setting
#Set second option - Site owners and members, and people with Edit permissions can share files and folders, but only site owners can share the site.
$Web.MembersCanShare = $true
$Web.Update()
$Web.Context.ExecuteQuery()
#Set "False" for AssociatedMemberGroup.AllowMembersEditMembership
$AssociatedMember = $web.AssociatedMemberGroup
$Context.Load($AssociatedMember)
$Context.ExecuteQuery()
$Web.AssociatedMemberGroup.AllowMembersEditMembership = $false
$Web.AssociatedMemberGroup.Update();
$Web.Context.ExecuteQuery()
Disable Sharing feature for Non-Owners (Members and Users with Edit permissions)
You can use the following PnP PowerShell script to disable the sharing permissions for members and allow only owners to share files and folders. This script enables the sharing permission option “Only site owners can share files, folders, and the site”.
#Provide SPO Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/sitename"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
$Web = Get-PnPWeb
$Context = Get-PnPContext
#Update Sharing permissions setting
#Set third option - Only site owners can share files, folders, and the site.
$Web.MembersCanShare = $false
$Web.Update()
$Web.Context.ExecuteQuery()
This was exactly what I was looking for! Thank you for the share!
This is the only place I could find this solution. thanks for sharing!