How to Activate Publishing Feature using PowerShell in SharePoint?

Requirement: SharePoint Publishing sites used to create Public websites, intranet portals, etc. We had to activate the publishing feature in SharePoint using PowerShell in scenarios, such as:

  • Activating Publishing feature through web user interface resulted in error!
  • Bulk activating publishing feature for all site collections and sites in a web application.

The publishing feature can be activated manually or through PowerShell scripts. In this post, we’ll show you how to use PowerShell to activate the feature.

PowerShell to Activate Publishing Feature in SharePoint:

Publishing sites rely on the “SharePoint Server Publishing Infrastructure” feature, which needs to be activated both at site collection and site level. To activate the publishing feature, do the following:

  • Navigate to Site Settings >> Click on the “Site collection features” link under “Site collection administration”
  • Click on the “Activate” button next to “SharePoint Server Publishing Infrastructure”
    sharepoint 2013 enable publishing infrastructure powershell

SharePoint PowerShell to activate publishing feature at site collection:

Here is the PowerShell to activate publishing feature at site collection.

Enable-SPFeature -Identity "PublishingSite" -url "https://intranet.crescent.com"

Let’s add some error handling and variables to it:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL="https://intranet.crescent.com"
$FeatureName = "PublishingSite"

#Check if publishing feature is already activated in the site collection
$Feature = Get-SPFeature -site $siteURL | Where-object {$_.DisplayName -eq $FeatureName}
if($Feature -eq $null)
{    
    #Enable the Publishing feature 
    Enable-SPFeature -Identity $FeatureName -url $SiteURL -Confirm:$False
   
    Write-host "Publishing Feature Activated on $($SiteURL)" -ForegroundColor Green    
}
else
{
    Write-host "Publishing Feature is already Active on $($SiteURL)" -ForegroundColor Red
}

Enable SharePoint server publishing at the site level:

We have to activate the publishing feature at site level also.

  • Go back to Site Settings page >> Click on “Manage Site Features” link under “Site Actions” group
  • Click on Activate button next to “SharePoint Server Publishing”

This enables publishing feature at web level. To activate SharePoint server publishing using PowerShell, use this script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$WebURL="https://intranet.crescent.com/Sales/"
$FeatureName = "PublishingWeb"

#Check if publishing feature is already activated in the site 
$Feature = Get-SPFeature -Web $WebURL | Where-object {$_.DisplayName -eq $FeatureName}
if($Feature -eq $null)
{    
    #Enable the Publishing feature 
    Enable-SPFeature -Identity $FeatureName -url $WebURL -Confirm:$False
   
    Write-host "Publishing Feature Activated on $($WebURL)" -ForegroundColor Green    
}
else
{
    Write-host "Publishing Feature is already Active on $($WebURL)" -ForegroundColor Red
}
Publishing feature must be enabled at site collection before enabling it to sub-sites!

Activate SharePoint server publishing infrastructure with PowerShell for all site collections & sites in a web application:

At times, You may have to activate or deactivate SharePoint server publishing infrastructure using PowerShell for all webs in the entire site collection or even all site collections in the entire web application. Here is the script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$WebAppURL="https://intranet.crescent.com"
$SiteFeatureName = "PublishingSite"
$WebFeatureName = "PublishingWeb"

#Get all site collections of the web app and iterate through
$SiteColl= Get-SPSite -WebApplication $WebAppURL -Limit ALL
Foreach($Site in $SiteColl)
{ 
    write-host "Processing site collection:"$Site.URL -ForegroundColor Yellow 
    #Check if publishing feature is already activated in the site 
    $Feature = Get-SPFeature -Site $Site.URL | Where-object {$_.DisplayName -eq $SiteFeatureName}

    if($Feature -eq $null)
    {    
        #Enable the Publishing feature 
        Enable-SPFeature -Identity $SiteFeatureName -url $Site.URL -Confirm:$False
   
        Write-host "Publishing Feature Activated on $($Site.URL)" -ForegroundColor Green    
    }
    else
    {
        Write-host "Publishing Feature is already Active on $($Site.URL)" -ForegroundColor Red
    }

    #Loop through each web in the site collection
    Foreach($Web in $Site.AllWebs)
    {
        write-host "Processing Web"$Web.URL -ForegroundColor Yellow
        #Check if publishing feature is already activated in the web 
        $Feature = Get-SPFeature -Web $Web.URL | Where-object {$_.DisplayName -eq $WebFeatureName}
        if($Feature -eq $null)
        {    
            #Enable the Publishing feature 
            Enable-SPFeature -Identity $WebFeatureName -url $Web.URL -Confirm:$False
   
            Write-host "Publishing Feature Activated on $($Web.URL)" -ForegroundColor Green    
        }
        else
        {
            Write-host "Publishing Feature is already Active on $($Web.URL)" -ForegroundColor Red
        }
    }
}

To activate a feature using PowerShell in SharePoint Online, use: How to Activate Publishing Feature in SharePoint Online using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *