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", "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 Deactivate 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 -ne $null)
    {
        #Disable site collection feature
        Disable-PnPFeature -Scope Site -Identity $FeatureId -Force
        Write-host -f Green "`tFeature Deactivated Successfully!"
    }
    Else
    {
        Write-host -f Cyan "`tFeature is not active!"
    }
}

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 -ErrorAction Stop
  
        #Check if the Feature is Activate
        If($Feature.DefinitionId -ne $null)
        {    
            #Deactivate feature            
            Disable-PnPFeature -Scope Web -Identity $FeatureId -Force -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 all webs
Get-PnPSubWeb -IncludeRootWeb -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 -ErrorAction Stop
 
        #Check if the Feature is activated
        If($Feature.DefinitionId -ne $null)
        {
            #Deactivate the feature
            Disable-PnPFeature -Scope Web -Identity $FeatureId -Force -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, Redirect, 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
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Call the Function for Web and all Subwebs
    Get-PnPSubWeb -IncludeRootWeb -Recurse | ForEach-Object { Deactivate-PnPWebFeature $_ -FeatureId $FeatureId} 
}

Conclusion

In conclusion, disabling a feature for all SharePoint Online sites using PowerShell can be a great time-saver and simplify the management of a large number of sites. By connecting to your tenant, retrieving the list of sites, verifying the feature’s state, and disabling the feature on each site, you can quickly and efficiently manage your SharePoint Online environment. It’s important to note that disabling a feature may affect the functionality of the SharePoint sites, so it’s recommended to thoroughly test the feature before disabling it for all the sites. Additionally, it’s always a good practice to have a backup plan for your data and configuration prior to making any changes in your SharePoint Online environment.

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 *