SharePoint Online: PowerShell to Change Access Request Email for All Sites

Requirement: Change Access Request Email in SharePoint Online.

sharepoint online change access request email using powershell

PowerShell to Change Access Request Email in SharePoint Online for a Site Collection:

Access requests allow users to request permission to access a SharePoint site or site collection. Access requests are sent to the site owner’s email address by default. However, you may want to change this email address to a different email address or group. This article will guide you through the steps to change the access request email in SharePoint Online.

Let’s set access request email for a SharePoint Online site collection using PowerShell:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Set-SPOAccessRequestEmail([String]$SiteURL, [String]$AccessReqEmail)
{  
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web and Sub Sites
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($web.AllProperties)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
 
        #Check if the web has unique permissions
        $Web.Retrieve("HasUniqueRoleAssignments")
        $Ctx.ExecuteQuery()

        If($Web.HasUniqueRoleAssignments -eq $true)
        {
            #Set Access request Email
            $Web.RequestAccessEmail = $AccessReqEmail
            $Web.Update()
            $Ctx.ExecuteQuery()
            Write-Host -f Green "Access Request Email Updated for the Site:" $($SiteURL)
        }
        Else
        {
            Write-Host -f Yellow "Site '$($SiteURL)' is inhering Permissions from the Parent"
        }

        #Iterate through each subsite of the current web
        ForEach ($Subweb in $Ctx.Web.Webs)
        {
            #Call the function recursively 
            Set-SPOAccessRequestEmail -SiteURL $Subweb.url -AccessReqEmail $AccessReqEmail
        }
    }
    Catch {
        write-host -f Red "Error Updating Access Request Email!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://Crescent.sharepoint.com"
$AccessReqEmail="Support@Crescenttech.com"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Call the function 
Set-SPOAccessRequestEmail -SiteURL $SiteURL -AccessReqEmail $AccessReqEmail

SharePoint Online: Change Access Request Email Address for All Sites in the Tenant

Let me show you how to use a PowerShell script to change the email address for all SharePoint Online sites. This is a convenient way to manage your organization’s email addresses and keep them consistent across all sites.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"

#Function to set access request Email of a site/site collection
Function Set-SPOAccessRequestEmail([String]$SiteURL, [String]$AccessReqEmail)
{  
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web and Sub Sites
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($web.AllProperties)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
 
        #Check if the web has unique permissions
        $Web.Retrieve("HasUniqueRoleAssignments")
        $Ctx.ExecuteQuery()

        If($Web.HasUniqueRoleAssignments -eq $true)
        {
            #Set Access request Email
            $Web.RequestAccessEmail = $AccessReqEmail
            $Web.Update()
            $Ctx.ExecuteQuery()
            Write-Host -f Green "Access Request Email Updated for the Site:" $($SiteURL)
        }
        Else
        {
            Write-Host -f Yellow "Site '$($SiteURL)' is inhering Permissions from the Parent"
        }

        #Iterate through each subsite of the current web
        ForEach ($Subweb in $Ctx.Web.Webs)
        {
            #Call the function recursively 
            Set-SPOAccessRequestEmail -SiteURL $Subweb.url -AccessReqEmail $AccessReqEmail
        }
    }
    Catch {
        write-host -f Red "Error Updating Access Request Email!" $_.Exception.Message
    }
}

#Change Access Request Settings for All Site collections in the Tenant- Including Modern Team sites and communication sites
Function Change-SPOTenantAccessRequestEmail($AdminSiteURL)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
 
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()

    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        Set-SPOAccessRequestEmail -SiteUrl $Site.Url -AccessReqEmail $AccessReqEmail
    }
}
 
#Set global Parameters
$AdminSiteUrl = "https://Crescent-admin.sharepoint.com"
$AccessReqEmail="Support@Crescenttech.com"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function 
Change-SPOTenantAccessRequestEmail -AdminSiteURL $AdminSiteUrl

This script changes the access request email for all site collections in SharePoint Online. Here is another post to change access request email or disable access request using PowerShell for a single SharePoint Online site: How to Change Access Request email or Disable Access Requests in SharePoint Online?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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