Enable Site and Web Features CSOM PowerShell Script Office 365 SharePoint 2013

Hi All, Today I will be sharing code to activate web and site features using CSOM and PowerShell script. I am following the same structure keeping the input values in XML file and run the script.

New to Office 365 SharePoint Online? Get Office 365 Enterprise E3 Subscription & Try out all the features

PS Code :

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “samtest@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web


function AddFeature([Microsoft.SharePoint.Client.Web] $web)
foreach ($FeatureSite in $xmldata.WebSite.FeaturesID.FeatureSite)
$featureIdSite = $FeatureSite.ID
$fId = [GUID]$featureIdSite
Enable-Feature $fId $True $FeatureSite.Scope


function Enable-Feature
[Parameter(Mandatory=$true, Position=1)]

[Parameter(Mandatory=$true, Position=2)]

[Parameter(Mandatory=$true, Position=3)]
Write-Host “Enabling feature $featureId on $featureDefinitionScope” -foregroundcolor black -backgroundcolor yellow

$featureGuid = new-object System.Guid $featureId

$features = $null

if ($featureDefinitionScope -eq [Microsoft.SharePoint.Client.FeatureDefinitionScope]::Site)

$features = $clientContext.Site.Features

} else {

$features = $clientContext.Web.Features


$feature = $features.Add($featureGuid, $force, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)

# TODO: Check if the feature is already enabled

Write-Host “Feature succesfully enabled” -foregroundcolor black -backgroundcolor green
AddFeature $web

XML Input Entries-

<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<FeatureSite ID=”f6924d36-2fa8-4f0b-b16d-06b7250180fa” Scope=”Site”>
<FeatureSite ID=”22a9ef51-737b-4ff2-9346-694633fe4416″ Scope=”Web”>

Read some SharePoint 2013 PowerShell tutorial:


Check out Best Alternative to InfoPath -> Try Now


(Installation & Features)

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →