SharePoint (2003 thru Online): Apply Sensitivity label to all Site collections in SharePoint admin center

Monday, June 10, 2024

Apply Sensitivity label to all Site collections in SharePoint admin center

With E3 license, we have to label everything manually, which is very time-consuming when we deal with many site collections.

For our project, I used a PowerShell Script to automate the process of applying Sensitivity labels to all Site Collections.

#Run the Get-Label command to retrieve the list of available labels:
Connect-IPPSSession -UserPrincipalName spadmin@gurram.onmicrosoft.com
Get-Label |ft Name, Guid, ContentType
#Copy the required label GUID
Disconnect-IPPSSession
--------------------------------------------------------------
#Connect to PnP PowerShell
Connect-PnPOnline -Url "https://gurram-admin.sharepoint.com" -Interactive

#Get All Site collections - Include Only: Team site (no M365 Group), Team site (classic experience), Project Web App site and Team sites
$Sites = Get-PnPTenantSite | Where-Object { $_.Template -eq "STS#3" -or $_.Template -eq "STS#0" -or $_.Template -eq "PWA#0" -or $_.Template -eq "GROUP#0" }

#For each site in the Site collections
ForEach ($site in $Sites) {
    #Required Label GUID from the above Script
    $LabelId = "abc123de-45f6-7g89-hi12-34j56789jk12"  
    $ctn = Connect-PnPOnline -Url $site.URL -Interactive
    $label = Get-PnPSiteSensitivityLabel -Connection $ctn

   if ($label.DisplayName -eq "") {
   #Add sensitivity Label to site
   Set-PnPTenantSite -Identity $site.URL -SensitivityLabel $LabelId
  } else {
    Write-Host $site.URL,"Not Blank"
  }
   $Object = [PSCustomObject]@{
    URL = $site.URL
    Sensitivitylabel= $label.DisplayName
    }
    $List += $Object
    #Write-Host $site.URL
    }
#Disconnect
Disconnect-PnPOnline

No comments:

Post a Comment