SharePoint Online: Disable a Feature for All Sites using PowerShell

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

If you are a SharePoint administrator, there may be times when you need to disable a feature for all sites in your organization. You can do this by using PowerShell! This blog post will show you how to disable a feature for all sites in your SharePoint Online tenant using PowerShell. This can be helpful if you have a feature that you no longer want to be activated in your tenant, and you do not want to do it manually for each site.

sharepoint online deactivate feature on all sites using powershell

PowerShell to Deactivate a Site Feature on All Collections in the Tenant

We have a requirement to disable the SharePoint Online “Open Documents in Client Applications by Default” feature for all sites in the tenant. Here is the PowerShell to disable 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 Deactivate 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 -ne $null)
    {
        #Disable site collection feature
        Disable-PnPFeature -Scope Site -Identity $FeatureId -Force -Connection $SiteConn
        Write-host -f Green "`tFeature Deactivated Successfully!"
    }
    Else
    {
        Write-host -f Cyan "`tFeature is not active!"
    }
    Disconnect-PnPOnline -Connection $SiteConn
}

How to Disable a Feature for All Sites in a Site Collection using PowerShell?

Say, we want to disable the “Following Content” site feature from all webs of a given site collection:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FeatureId = "a7a2793e-67cd-4dc1-9fd0-43f61581207a" #"Following Content" Web Scoped Feature
 
#Function to Deactivate a Feature in SharePoint Online Web
Function Deactivate-PnPWebFeature
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web,
        [parameter(Mandatory = $true, ValueFromPipeline = $False)] $FeatureId
    )
 
    Try {
        Write-host -f Yellow "Trying to Deactivate Feature on:"$web.Url
        #Get the Feature to Deactivate
        $Feature = Get-PnPFeature -Scope Web -Identity $FeatureId -Web $Web -ErrorAction Stop
  
        #Check if the Feature is Activate
        If($Feature.DefinitionId -ne $null)
        {    
            #Deactivate feature            
            Disable-PnPFeature -Scope Web -Identity $FeatureId -Force -Web $Web -Verbose -ErrorAction Stop
  
            Write-host -f Green "`tFeature Deactivated Successfully!"
        }
        Else
        {
            Write-host -f Cyan "`tFeature is not 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 Web & all Subwebs
Get-PnPWeb | Deactivate-PnPWebFeature -FeatureId $FeatureId
Get-PnPSubWeb -Recurse | ForEach-Object { Deactivate-PnPWebFeature $_ -FeatureId $FeatureId} 

SharePoint Online: Deactivate a feature on All Sites using PowerShell

We can deactivate a web scoped feature from all sites and subsites of the tenant with the below PowerShell.

#Parameters
$TenantAdminURL = "https://Crescent-Admin.sharepoint.com"
$FeatureId = "a7a2793e-67cd-4dc1-9fd0-43f61581207a" #"Following Content" Web Scoped Feature
 
#Function to Deactivate a Web Feature in SharePoint Online
Function Deactivate-PnPWebFeature
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web,
        [parameter(Mandatory = $true, ValueFromPipeline = $False)] $FeatureId
    )
 
    Try {
        Write-host -f Yellow "Trying to Deactivate Feature on:"$web.Url
        #Get the Feature to Deactivate
        $Feature = Get-PnPFeature -Scope Web -Identity $FeatureId -Web $Web -Connection $SiteConn -ErrorAction Stop
 
        #Check if the Feature is activated
        If($Feature.DefinitionId -ne $null)
        {
            #Deactivate the feature
            Disable-PnPFeature -Scope Web -Identity $FeatureId -Force -Web $Web -Connection $SiteConn -ErrorAction Stop
            Write-host -f Green "`tFeature Deactivated Successfully!"
        }
        Else
        {
            Write-host -f Cyan "`tFeature is not active!"
        }
    }
    Catch {
        write-host "`tError Deactivating 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 and all Subwebs
    Get-PnPWeb -Connection $SiteConn | Deactivate-PnPWebFeature -FeatureId $FeatureId
    Get-PnPSubWeb -Connection $SiteConn -Recurse | ForEach-Object { Deactivate-PnPWebFeature $_ -FeatureId $FeatureId}
 
    Disconnect-PnPOnline -Connection $SiteConn  
}

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!

Leave a Reply