SharePoint Online: Activate a Feature on All Sites using PowerShell

Requirement: Enable a SharePoint Online feature for all sites using PowerShell.

sharepoint online powershell enable feature on all site collections

PowerShell to Activate a Site Collection Feature for All Sites

Do you want to activate the feature on all your SharePoint Online sites? Well, this guide will show you how to activate a feature on all SharePoint Online sites using PowerShell. We have a requirement to enable a SharePoint Online “Open Documents in Client Applications by Default” feature for all site collections in the tenant. Here is the PowerShell to enable a feature for all site collections:

#Parameters
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
#Site Collection feature "Open Documents in Client Applications by Default"
$FeatureId = "8a4b8de2-6fd8-41e9-923c-c7c3c00f8295" 

#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
    Write-host -f Yellow "Trying to Activate the feature on site:"$Site.Url    
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred

    #Get the Feature
    $Feature = Get-PnPFeature -Scope Site -Identity $FeatureId

    #Check if feature is activated
    If($Feature.DefinitionId -eq $null)
    {
        #Enable site collection feature
        Enable-PnPFeature -Scope Site -Identity $FeatureId -Force

        Write-host -f Green "`tFeature Activated Successfully!"
    }
    Else
    {
        Write-host -f Cyan "`tFeature is already active!"
    }
}

This PowerShell script activates a feature on all sites in your SharePoint Online tenant. This can be useful if you need to quickly activate the feature on multiple sites. BTW, You need site collection administrator privileges to complete these steps.

Enable a Site Feature in All Sites of a SharePoint Online Site Collection using PowerShell

What if you want to activate a web-scoped feature on all sites, including the root site and subsites of a given site collection? Say, we want to activate the “Publishing” feature on all sites (webs) in a SharePoint Online site collection.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FeatureId = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" #Web Scoped Publishing Feature
 
#Function to Activate a Feature in SharePoint Online Web
Function Activate-PnPWebFeature
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web,
        [parameter(Mandatory = $true, ValueFromPipeline = $False)] $FeatureId
    )
 
    Try {
        Write-host -f Yellow "Trying to Activate Feature on:"$web.Url
        #Get the Feature to activate
        $Feature = Get-PnPFeature -Scope Web -Identity $FeatureId -ErrorAction Stop
  
        #Check if the Feature is Activate
        If($Feature.DefinitionId -eq $null)
        {    
            #Activate feature            
            Enable-PnPFeature -Scope Web -Identity $FeatureId -Force -Verbose -ErrorAction Stop
  
            Write-host -f Green "`tFeature Activated Successfully!"
        }
        Else
        {
            Write-host -f Cyan "`tFeature is already active!"
        }
    }
    Catch {
        write-host "`tError Activating Feature: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Connect to the site collection
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Call the Function for Root Web & all Subwebs
Get-PnPSubWeb -IncludeRootWeb -Recurse | ForEach-Object { Activate-PnPWebFeature $_ -FeatureId $FeatureId}

If your web scoped feature has any dependency on the site collection features, You must activate the site collection feature first! In our case, We must activate the publishing feature at the site collection before activating that feature on all webs.

PowerShell to Activate Feature On All Sites of the Tenant

How about activating a site feature for all sites in the tenant? The below PowerShell script enables a given feature on all sites, including the root site and its sub-sites of all site collections in the SharePoint Online tenant.

#Parameters
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
$FeatureId = "a7a2793e-67cd-4dc1-9fd0-43f61581207a" #"Following Content" Web Scoped Feature
 
#Function to Activate a Web Feature in SharePoint Online
Function Activate-PnPWebFeature
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web,
        [parameter(Mandatory = $true, ValueFromPipeline = $False)] $FeatureId
    )
 
    Try {
        Write-host -f Yellow "Trying to Activate Feature on:"$web.Url
        #Get the Feature to activate
        $Feature = Get-PnPFeature -Scope Web -Identity $FeatureId -ErrorAction Stop
 
        #Check if the Feature is Activate
        If($Feature.DefinitionId -eq $null)
        {
            #Activate the feature
            Enable-PnPFeature -Scope Web -Identity $FeatureId -Force -ErrorAction Stop 
            Write-host -f Green "`tFeature Activated Successfully!"
        }
        Else
        {
            Write-host -f Cyan "`tFeature is already active!"
        }
    }
    Catch {
        write-host "`tError Activating Feature: $($_.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
    $SiteConn = Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Call the Function for Web & all Subwebs
    Get-PnPSubWeb -IncludeRootWeb -Recurse | ForEach-Object { Activate-PnPWebFeature $_ -FeatureId $FeatureId} 
}

If you are looking to activate a feature on a specific site, please see this article, use: SharePoint Online: Activate a Feature using PowerShell

You can also enable a SharePoint Online feature in all site collections using PowerShell with SharePoint Online Management Shell and CSOM as in my other article: PowerShell to enable a site collection feature for all sites in SharePoint Online

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!

Leave a Reply

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