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 " 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 = ""
            $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 = ""
            $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 running this script! Otherwise, You may get "Access denied" or "401 unauthorized" errors! Once access requests is 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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.