SharePoint Online: Disable Access Requests for All Sites using PowerShell

Requirement: Disable access requests for SharePoint Online sites

How to Disable Access Request 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:

  • 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 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 Root Web and all Subwebs
Get-PnPWeb -Includes HasUniqueRoleAssignments | Disable-PnPAccessRequest
Get-PnPSubWeb -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", "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-PnPSubWeb -Connection $SiteConn -Recurse -Includes HasUniqueRoleAssignments | ForEach-Object { Disable-PnPAccessRequest $_ } 
    Disconnect-PnPOnline -Connection $SiteConn
}

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 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!

3 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