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
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", "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    
    $SiteConn = Connect-PnPOnline -Url $Site.Url -Credentials $Cred

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

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

        Write-host -f Green "`tFeature Activated Successfully!"
    }
    Else
    {
        Write-host -f Cyan "`tFeature is already active!"
    }
    Disconnect-PnPOnline -Connection $SiteConn
}

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 root site and subsites of a given site collection? Say, we want to activate "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 -Web $Web -ErrorAction Stop
 
        #Check if the Feature is Activate
        If($Feature.DefinitionId -eq $null)
        {    
            #Activate feature            
            Enable-PnPFeature -Scope Web -Identity $FeatureId -Force -Web $Web -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 -UseWebLogin

#Call the Function for Web & all Subwebs
Get-PnPWeb | Activate-PnPWebFeature -FeatureId $FeatureId 
Get-PnPSubWebs -Recurse | ForEach-Object { Activate-PnPWebFeature $_ -FeatureId $FeatureId}
If your web scoped feature has any dependency on site collection features, You must activate the site collection feature first! In our case, We must activate publishing feature at the site collection prior activating that feature in 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 given feature on all sites, including 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 -Web $Web -Connection $SiteConn -ErrorAction Stop

        #Check if the Feature is Activate
        If($Feature.DefinitionId -eq $null)
        {
            #Activate the feature
            Enable-PnPFeature -Scope Web -Identity $FeatureId -Force -Web $Web -Connection $SiteConn -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", "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-PnPWeb -Connection $SiteConn | Activate-PnPWebFeature -FeatureId $FeatureId 
    Get-PnPSubWebs -Connection $SiteConn -Recurse | ForEach-Object { Activate-PnPWebFeature $_ -FeatureId $FeatureId}

    Disconnect-PnPOnline -Connection $SiteConn   
}
To enable a feature in SharePoint Online site, 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 another article: PowerShell to enable a site collection feature for all sites in SharePoint Online

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.