Disabling Access Requests and Controlling Site Sharing with PowerShell

Introduction

In SharePoint Online, managing access requests and controlling site sharing settings is crucial for maintaining security and governance. In this article we will see how to disable access requests and site sharing permission settings for a SharePoint site using PowerShell.

What is PnP PowerShell?

PnP PowerShell is a popular PowerShell module that simplifies managing SharePoint and Office 365 environments, providing a seamless scripting experience for automating tasks and configurations.

Prerequisites:

  • PnP PowerShell module installed
  • Permissions: Ensure that you have the necessary permissions to modify site permissions settings from your SharePoint site
  • Update the SharePoint site URL in the script

Below are the steps to hide the export to excel button from the list view

Step 1. Connect to your SharePoint site

To begin, establish a connection to your SharePoint site using the Connect-PnPOnline cmdlet. Provide the URL of your site and your credentials when prompted.

Connect-PnPOnline "https://yourdomain.sharepoint.com/sites/yoursitecollection" -Interactive

Step 2. Retrieve the Web

Next, we will retrieve the web object using below cmdlet. It will allow us to access and modify the web properties.

$Web = Get-PnPWeb

Step 3. Disable Access Requests

Access Requests settings enables users to request access to the SharePoint site if they don’t already have permissions. In this step, we’ll disable access requests by settings the ‘RequestAccessEmail’ property of the web object to empty string and call the ‘SetUseAccessRequestDefaultAndUpdate’ method to update the access request settings and save the changes using the Update method.

$Web.RequestAccessEmail = [string]::Empty
$Web.SetUseAccessRequestDefaultAndUpdate($False)
$Web.Update()
$Web.Context.ExecuteQuery()

Step 4. Disable Site Sharing Permission Settings:

In order to prevent non-owners from sharing the site, we’ll use below cmdlet to update the site sharing permission settings for non-owners.

Set-PnPSite -Identity "https:// yourdomain.sharepoint.com/sites/ yoursitecollection " -DisableSharingForNonOwners

Step 5. Disconnect from SharePoint site

Once the email is sent, it's a good practice to disconnect from the SharePoint site using the Disconnect-PnPOnline cmdlet.

Disconnect-PnPOnline

Complete script

#Step 1 Connect SharePoint site
Connect-PnPOnline "https://yourdomain.sharepoint.com/sites/ yoursitecollection" -Interactive

#Step 2 Get the Web
$Web = Get-PnPWeb

#Step 3  disable access requests
$Web.RequestAccessEmail = [string]::Empty
$Web.SetUseAccessRequestDefaultAndUpdate($False)
$Web.Update()
$Web.Context.ExecuteQuery()

#Step 4 disable Site Sharing Permission Settings
Set-PnPSite -Identity "https://yourdomain.sharepoint.com/sites/ yoursitecollection" -DisableSharingForNonOwners

#Step 5 Disconnect SharePoint site
Disconnect-PnPOnline

Output

Disabling Access Requests and Controlling Site Sharing with PowerShell

Conclusion

In this article, we have learned how to use PowerShell to disable access request settings and prevent non-owners from sharing a SharePoint site. By running this script, you can modify the site permission settings to enhance the security and governance of your SharePoint site, preventing unauthorized access and controlling sharing capabilities.

I hope this article  post has provided you with a clear understanding of how to leverage PowerShell to disable access requests and control site sharing permissions in SharePoint Online. Feel free to reach out with any questions or feedback.


Similar Articles