Script to activate SharePoint feature on all site collection – Combine PnP and CSOM scripts

The prerequisite for this script is you need to be global admin on your tenant or you you have app installed with full permission on tenant scope

Let’s assume that your app have full permission on tenant scope, and you know the client id/client secret of your app

1. If your powersell script is not support for the execution policy, add this to your powershell windows first:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

2. Import SharePointPnPPowerShellOnline and Microsoft.Online.SharePoint.Powershell

Import-Module SharePointPnPPowerShellOnline -Scope “Local”
Import-Module Microsoft.Online.SharePoint.Powershell

Add-Type -Path “$path to client dlls\Microsoft.SharePoint.Client.dll”
Add-Type -Path “$path to client dlls\Microsoft.SharePoint.Client.Runtime.dll”

3. Declare the parameters

$global:appId = “your-client-id-guid-string”
$global:appSecret = “your-client-secret”

$global:adminUrl = “”

$global:SPFeatureId = “any-sharepoint-feature-id-fbace37b4a34”;

4. Connect to admin URL to get all site collection

Connect-PnPOnline -AppId $global:appId -AppSecret $global:appSecret -Url $global:adminUrl

$adminConnection = Get-PnPConnection

$allSitecollections = Get-PnPTenantSite -Connection $adminConnection

$sitecollections = New-Object System.Collections.ArrayList

5. For each site collection, connect again and activate the feature

foreach($siteCollection in $allSitecollections)
$targetUrl = $siteCollection.Url
Connect-PnPOnline -AppId $global:appId -AppSecret $global:appSecret -Url $targetUrl
$connection = Get-PnPConnection

#Activate SPFeature
$ctx = Get-PnPContext #This is important to get client content and use in CSOM
$site = $

$Site.Features.Add($global:SPFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null

Disconnect-PnPOnline -ErrorAction SilentlyContinue

That’s all!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s