SharePoint Online: Disable Access Requests for All Sites using PowerShell

Requirement: Disable access requests for SharePoint Online sites.

How to Disable Access Requests in SharePoint Online?

You may want to disable access requests in SharePoint Online in specific scenarios. We can disable the ability for people to request access to your SharePoint Online site, either from site settings or by using PowerShell. In this blog post, we’ll walk through how to use PowerShell to disable (or enable) user access requests in SharePoint Online. Let’s get started!

To disable access requests for a SharePoint Online site, do the following:

  1. Go to the SharePoint Online site where you would like to disable access requests.
  2. Click on the Settings gear icon in the upper-right corner, and then select “Site Permissions” in the drop-down menu.
  3. On the Site Permissions panel, click on the “Change how members can share” link under “Site Sharing”.
    how to disable access requests in sharepoint online
  4. Switch the “Access Request” settings to “OFF” by sliding the button next to it.
    sharepoint online powershell disable access requests
  5. Click on the “Save” button to commit your changes.

That’s all! We have disabled the access requests for your site. This means that users will not be able to request access to resources for they do not have permission. Similarly, on classic SharePoint sites (and modern sites too!), follow these steps to turn off access requests.

  1. Click on Settings gear >> Site Permissions >> Access Requests Settings.
  2. This takes you to the SharePoint Online access request settings page of the site. Uncheck the “Allow access requests” checkbox and click on OK to save your changes.
    sharepoint online disable access request powershell

This disables the access requests for the specific site. However, access request settings are scoped at each site where users have unique permissions. So, If your subsite uses unique permissions, then you have to repeat the above step for each subsite in your site collection (or each subsite in the tenant!). Let’s automate these steps to disable access requests for the site collection and tenant levels.

SharePoint Online: Disable Access Request using PowerShell

Using PnP PowerShell, let’s disable access requests for a given SharePoint Online site collection.

#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"

#Function to disable access request on SharePoint Online Web
Function Disable-PnPAccessRequest
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web
    )

    Try {
        Write-host -f Yellow "Disabling Access Request on:"$web.Url
        If($Web.HasUniqueRoleAssignments)
        {
            #Disable Access Request           
            $Web.RequestAccessEmail = [string]::Empty
            $Web.SetUseAccessRequestDefaultAndUpdate($False)
            $Web.Update()
            Invoke-PnPQuery
            Write-host -f Green "`tAccess Request has been Disabled!"$web.Url
        }
        else
        {
            Write-host -f Yellow "`tWeb inherits permissions from the parent!"$web.Url
        }
    }
    Catch {
        write-host "`tError Disabling Access Request: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Call the Function for all webs
Get-PnPSubWeb -IncludeRootWeb -Recurse -Includes HasUniqueRoleAssignments | ForEach-Object { Disable-PnPAccessRequest $_ }

How about disabling access requests for all sites in the tenant?

PowerShell to Disable Access Request for All Sites in the Tenant

Let me show you how to automate disabling access requests in all SharePoint Online sites in the tenant. This PowerShell script iterates through each site in the tenant and turns-OFF access requests if the site uses unique permissions:

#Parameter
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
 
#Function to disable access request on SharePoint Online Web
Function Disable-PnPAccessRequest
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web
    )
 
    Try {
        Write-host -f Yellow "Disabling Access Request on:"$web.Url
        If($Web.HasUniqueRoleAssignments)
        {
            #Disable Access Request           
            $Web.RequestAccessEmail = [string]::Empty
            $Web.SetUseAccessRequestDefaultAndUpdate($False)
            $Web.Update()
            Invoke-PnPQuery
            Write-host -f Green "`tAccess Request has been Disabled!"$web.Url
        }
        else
        {
            Write-host -f Yellow "`tWeb inherits permissions from the parent!"$web.Url
        }
    }
    Catch {
        write-host "`tError Disabling Access Request: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
  
#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
  
#Loop through each site collection
ForEach($Site in $SitesCollections)
{
    #Connect to site collection
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Call the Function for all webs
    Get-PnPSubWeb -IncludeRootWeb -Recurse -Includes HasUniqueRoleAssignments | ForEach-Object { Disable-PnPAccessRequest $_ }

}

Make sure you have access to all sites in your tenant before running this script! Otherwise, You may get “Access denied” or “401 unauthorized” errors! Once access requests are turned-off, users with no access to the site would see this page,

sharepoint online disable access request

instead of:

sharepoint online enable access request

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

4 thoughts on “SharePoint Online: Disable Access Requests for All Sites using PowerShell

  • SetUseAccessRequestDefaultAndUpdate is not working for me. I am looking to disabled access request from users, so that no user should be able to request access from owner. In my csom code runs and does not give any error but when i go back to the site UI, access request setting is still enabled. We are sharepoint online environment. Did this code work for you?

    Reply
    • Do you get the “Access Request has been Disabled! URL” message? If a site inherits permissions from its parent, You have to set these settings at the parent site.

      Reply
  • This is super helpful for disabling for existing sites. Do you know if there is a way or a setting to disable for future sites that are created in a site collection? I would like to prevent this feature in all new sites created in a specific site collection, but when I templated a site with the feature disabled, it didn’t stick within the template.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *