Thursday, 19 August 2010

Bulk deactivate feature and remove farm solution in PowerShell

As a follow up to my article back in June on how to install a farm solution and bulk activate site or site collection features in SharePoint 2010 using PowerShell, here I am doing the opposite by bulk deactivating a feature before removing the solution from the farm.

First, we get the feature from the farm with this line:

$feature = Get-SPFeature "FeatureName"

Then we have one of four scenarios to use for deactivation of the feature:

  • Deactivate site scoped feature in one site collection

$site = Get-SPSite http://portal
Disable-SPFeature $feature -Url $site.Url -Force -Confirm:$false
$site.Dispose()

  • Deactivate site scoped feature for all site collections in a Web Application (checks to see if the feature is activated before attempting to deactivate)

$webApp = Get-SPWebApplication -Identity http://portal
$webApp | Get-SPSite -limit all | ForEach-Object {
    if ($_.Features[$feature.ID]) {
        Disable-SPFeature $feature -Url $_.Url -Force -Confirm:$false
    }
}

  • Deactivate web scoped feature in one site

$web = Get-SPWeb http://portal
Disable-SPFeature $feature -Url $web.Url -Force -Confirm:$false
$web.Dispose()

  • Deactivate web scoped feature for all sites in a site collection (checks to see if the feature is activated before attempting to deactivate)

$site = Get-SPSite http://portal
$site | Get-SPWeb -limit all | ForEach-Object {
    if ($_.Features[$feature.ID]) {
        Disable-SPFeature $feature -Url $_.Url -Force -Confirm:$false
    }
}
$site.Dispose()

To remove the solution from the farm, use the script below. Note that there is a pause in the script whilst it waits for the solution to be uninstalled from the farm before attempting to remove it:

#Set up Web Application variable
#Only needed if solution contains Web Application scoped resources
$webApp = Get-SPWebApplication -Identity http://portal

#Get Solution from the farm
$solution = Get-SPSolution -Identity "Solution.wsp"

#Uninstall solution
#Add -WebApplication $webApp if solution contains Web Application scoped resources
Uninstall-SPSolution $solution -Confirm:$true

#Wait for solution to be uninstalled
do {Start-Sleep -s 1} while ($solution.Deployed -eq $true)

#Remove solution from the farm
Remove-SPSolution $solution -Confirm:$true

2 comments:

  1. Great, just what I needed to automate the removal of farm features in SharePoint 2010 :)

    ReplyDelete
  2. Uninstall-SPFeature ? / For safety deleting existing files ?

    ReplyDelete