1
$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" } 
#This script enables a feature on every site collection on a given web app

if ($snapin -eq $null) 
{ 
    Add-PSSnapin "Microsoft.SharePoint.Powershell" 
}

. .\environment.ps1

$featureName = "XXX.Publishing_SiteDisplayTemplates"

Get-SPFeature | where { $_.DisplayName -like $featureName }
$featureID = Get-SPFeature | where { $_.DisplayName -like $featureName } | Select Id

Write-host "Site: " $hostUrlPowershell



 $sites =  get-spsite -webapplication $hostUrlPowershell -Limit ALL
 $feature = $featureName #this might not be what you expect it to be, best to do get-spfeature | Select displayname

  Foreach ($oneweb in $sites)
  {
    write-host $oneweb
    $siteFeature = get-spfeature -site $oneweb | Where {$_.displayname -eq $feature}
    if ($siteFeature -eq $null)
    {
      Write-Host "Feature is not activated on $oneweb" -foregroundcolor Yellow
      Enable-SPFeature -Identity $Feature -URL $oneweb.URL -Confirm:$False
      #Disable-SPFeature -Identity $Feature -URL $oneweb.URL -Confirm:$False
    }
    else
    {
      Write-Host "Feature $feature is already activated on $oneweb, reactivating feature" -foregroundcolor green
      Disable-SPFeature -Identity $Feature -URL $oneweb.URL -Confirm:$False
      Enable-SPFeature -Identity $Feature -URL $oneweb.URL -Confirm:$False

    }
  }

I'm trying to reactivate a feature so I get the newest display templates available. But it seems that Powershell is only disable SPFeature. How can I wait for Disable-SPFeature to be done before enable the feature?

2

In this scenario you can actually use the -force attribute on Enable-SPFeature, since it will reenable the feature.

But I normally uses your approach, but I use the ID of the feature and not the actual $Feature object:

$FeatureId = $Feature.Id
Disable-SPFeature $FeatureId -URL $oneweb.URL -Confirm:$False
Enable-SPFeature -$FeatureId -URL $oneweb.URL -Confirm:$False
0

Note: This will only work if you are NOT using update-spsolution. If you are updating the solution using this command, you'll need to disable then enable for the changes to take effect.

https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/enable-spfeature?view=sharepoint-ps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.