Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

PowerShell: How do I deactivate a feature at the site collection level as well as all subsites?

$
0
0
I am trying to use PowerShell to deactivate a feature at site collection level as well as any subsites. Currently my code is only doing the deactivation at the site collection level, but not the subsites. Here is what my code looks like
Add-PSSnapin Microsoft.SharePoint.PowerShell
Function global:Get-SPWebApplication($WebAppURL)
{
 return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}

$Farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$FarmFeatures= $Farm.FeatureDefinitions
$WebAppURL = "http://server.com"
$WebApp = Get-SPWebApplication $WebAppURL
$SitesColl = $WebApp.Sites
$FeatureID = "FollowingContent"

#Iterate through each site collection/sub-site
if($SitesColl -ne $null)
{
    foreach($Site in $SitesColl)
    {
        $_ = $Site.Url

        If ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Farm)
        {
            $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Farm -ErrorAction SilentlyContinue
	        If ($IsActiveFeature -eq $null)
	        {
		        write-host "The specified feature ($FeatureID) is not activated on the Farm."
		        return;
	        }
            Disable-SPFeature -identity "FollowingContent" -confirm:$false
        }
        ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::WebApplication)
        {
	        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -WebApplication $WebAppURL -ErrorAction SilentlyContinue
            If ($IsActiveFeature -eq $null)
	        {
		        write-host "The specified feature ($FeatureID) is not activated in the Web application ($WebApplicationUrl)."
		        return;
	        }
            Disable-SPFeature -identity "FollowingContent" -URL "$WebAppURL" -confirm:$false
        }
        ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Site)
        {
            $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Site $_ -ErrorAction SilentlyContinue
            If ($IsActiveFeature -eq $null)
            {
	            write-host "The feature $FeatureID is not activated on the site: " $Site.Url
	            return;
            }
            Disable-SPFeature -Identity $FeatureID  -Url $_ -confirm:$false
        }
        ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Web)
        {

            $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Web $_ -ErrorAction SilentlyContinue
            If ($IsActiveFeature -eq $null)
            {
	            write-host "The feature $FeatureID is not activated on the site: " $Site.Url
	            return;
            }
            Disable-SPFeature -Identity $FeatureID  -Url $_ -confirm:$false
        }
    }
}
I know why it does not deactivate the feature at the sub site level. Any ideas? Thanks

Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>