We came across a situation recently where we needed to enable Publishing (at the web level – ie. It was already on at the site collection level) using PowerShell. This was in the context of a script that created many Sub Sites (webs) based on an automated process.
A good time to remind everyone that using Sub Sites is generally bad practice these days with SharePoint. Site Collections are a far better idea. There are certain exceptions to that (such as the case that lead to the script in this blog post). Take a look at Sean Wallbridge’s post for more information https://itgroove.net/brainlitter/2015/06/01/site-collections-for-the-win/.
# LOAD SHAREPOINT SDK (whichever method you prefer)
$siteCollectionUrl = “<site collection URL here>”
$username = “<username here>”
# Prompt for password at runtime.
$password = Read-Host -Prompt “Enter password” -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteCollectionUrl)
$clientContext.Credentials = $credentials
$FeatureId = [GUID](“94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb”)
$webFeatures = $clientContext.Web.Features
$webfeatures.Add($featureId, $force, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)