Friday 7 November 2014

Remove site collection and site features from content database in SharePoint using PowerShell

The scripts are used to remove the features from content base in SharePoint.

RemoveFeature.ps1

Param($ContentDb,$FeatureId,$ReportOnly);
Add-PSSnapin Microsoft.SharePoint.PowerShell

function Remove-SPFeatureFromContentDB($ContentDb,$FeatureId,$ReportOnly)
{
    $db = Get-SPDatabase | where { $_.Name -eq $ContentDb }
    [bool]$report = $false
    if ($ReportOnly -eq "yes") { $report = $true }
   
    $db.Sites | ForEach-Object {
       
        Remove-SPFeature -obj $_ -objName "site collection" -featId $FeatureId -report $report
               
        $_ | Get-SPWeb -Limit all | ForEach-Object {
           
            Remove-SPFeature -obj $_ -objName "site" -featId $FeatureId -report $report
        }
    }
}

function Remove-SPFeature($obj,$objName,$featId,[bool]$report)
{
    $feature = $obj.Features[$featId]
   
    if ($feature -ne $null) {
        if ($report) {
            write-host "Feature found in" $objName ":" $obj.Url -foregroundcolor Red
        }
        else
        {
            try {
                $obj.Features.Remove($feature.DefinitionId, $true)
                write-host "Feature successfully removed from" $objName ":" $obj.Url -foregroundcolor Red
            }
            catch {
                write-host "There has been an error trying to remove the feature:" $_
            }
        }
    }
    else {
        #write-host "Feature ID specified does not exist in" $objName ":" $obj.Url
    }
}


Start-Transcript

Remove-SPFeatureFromContentDB $ContentDb $FeatureId $ReportOnly

Stop-Transcript


RemoveFeature.bat

echo off
cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell {Set-ExecutionPolicy Unrestricted}
cd /d %~dp0

SET ContentDb="SharePoint_Content_Portal"
SET FeatureId="8096285f-1463-42c7-82b7-f745e5bacf29"
SET ReportOnly="yes"


powershell.exe -command .\RemoveFeature.ps1 '%ContentDb%' '%FeatureId%' '%ReportOnly%'
powershell {Set-ExecutionPolicy Restricted}

pause

1 comment:

  1. It didn't works on Sharepoint 2013!
    Any other solutions for this version?

    ReplyDelete