SharePoint Online: Disable Access Requests for All Sites using PowerShell

Requirement: Disable access request for SharePoint Online sites.

How to Disable Access Request in SharePoint Online?

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

  • Click on Settings gear >> Site Permissions >> Access Requests Settings 
  • 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 that users 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 request for both site collection and at tenant levels.

SharePoint Online: Disable Access Request using PowerShell
Let’s disable access request for a given SharePoint Online site collection using PnP PowerShell.

 #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 -UseWebLogin

#Call the Function for Root Web and all Subwebs
Get-PnPWeb -Includes HasUniqueRoleAssignments | Disable-PnPAccessRequest
Get-PnPSubWebs -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

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", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
  
#Loop through each site collection
ForEach($Site in $SitesCollections)
{
    #Connect to site collection
    $SiteConn = Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Call the Function for Root Web and all Subwebs
    Get-PnPWeb  -Connection $SiteConn -Includes HasUniqueRoleAssignments| Disable-PnPAccessRequest
    Get-PnPSubWebs -Connection $SiteConn -Recurse -Includes HasUniqueRoleAssignments | ForEach-Object { Disable-PnPAccessRequest $_ } 
    Disconnect-PnPOnline -Connection $SiteConn
}

Make sure you have access to all sites in your tenant prior to 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 is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “SharePoint Online: Disable Access Requests for All Sites using PowerShell

  • September 20, 2021 at 10:51 PM

    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