Activate Publishing Feature With PowerShell

Maybe you also struggled about this, too. Trying to activate the SharePoint Publishing feature results in an error and it’s not possible to activate it over the SharePoint UI.
In this case the PowerShell script below will do the activation. The script will also update a property of the web, indicating that the feature is beeing active.

If you wish, you can check the PropertyBag of the Web with SharePoint Manager. It will look like this:


## Force activating Publishing feature on web level

# Loading the SharePoint PowerShell Snapin if not already loaded
if ((Get-PsSnapin | ?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
   $PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# define variables  
$targetWebUrl = "http://myFarmURL/SubWeb"

# Force feature activation
Enable-SPFeature -Identity "PublishingWeb" -Url $targetWebUrl -Force

# Sometimes, somehow the web property is still displayed as "False" and needs to be set to "True"
$myWeb = Get-SPWeb $targetWebUrl
$myWeb.AllProperties["__PublishingFeatureActivated"] = "True"