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.PowershellAdd-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 = “https://yoursharepoint-admin.sharepoint.com”
$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 = $ctx.site
$Ctx.ExecuteQuery()$Site.Features.Add($global:SPFeatureId, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
$Ctx.ExecuteQuery()Disconnect-PnPOnline -ErrorAction SilentlyContinue
}
That’s all!!!