SharePoint Online: Disable a Feature for All Sites using PowerShell

Requirement: Disable a feature for all SharePoint Online sites using PowerShell.
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 "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 -UseWebLogin

#Call the Function for Web & all Subwebs
Get-PnPWeb | Deactivate-PnPWebFeature -FeatureId $FeatureId 
Get-PnPSubWebs -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-PnPSubWebs -Connection $SiteConn -Recurse | ForEach-Object { Deactivate-PnPWebFeature $_ -FeatureId $FeatureId}

    Disconnect-PnPOnline -Connection $SiteConn   
}

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.