Back to script library
Entra / Microsoft 365 · SharePoint & OneDrive

Update spo sites with labels

A script to check SharePoint Online team sites created for Microsoft 365 Groups that don't seem to have received sensitivity labels.

Connect & set up

Run these once per session. All scopes are read-only unless the script makes changes.

Connect-ExchangeOnline
Connect-SPOService -Url https://tenant-admin.sharepoint.com

Run it

The main script. Copy it, or download the .ps1 and run it from your console.

$ModulesLoaded = Get-Module | Select Name
If (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"; break}
If (!($ModulesLoaded -match "Microsoft.Online.Sharepoint.PowerShell")) {Write-Host "Please connect to the SharePoint Online Management module and then restart the script"; break}
CLS
[array]$Sites = Get-SPOSite -Limit All -Template Group#0
If (!($Sites)) { Write-Error "No sites for Microsoft 365 Groups found... exiting!" ; break}
Else { Write-Host ("Processing {0} sites" -f $Sites.Count) }
$SitesNoLabels = [System.Collections.Generic.List[Object]]::new()
ForEach ($Site in $Sites) { #Check each site to see if it has a sensitivity label
$SiteData = Get-SPOSite -Identity $Site.Url
If ([string]::IsNullOrWhiteSpace(($SiteData.SensitivityLabel)) -eq $True) {
Write-Host ("Site {0} has no label" -f $SiteData.Url)
$SiteInfo = [PSCustomObject][Ordered]@{
URL = $SiteData.Url
Title = $SiteData.Title }
$SitesNoLabels.Add($SiteInfo) }
} #End ForEach Sites
Write-Host ("Finished checking. {0} of {1} sites for Microsoft 365 Groups have no sensitivity label" -f $SitesNoLabels.Count, $Sites.Count)
Write-Host "Retrieving sensitivity label information for Microsoft 365 Groups"
[array]$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups = $Groups | ? {$_.SharePointSiteUrl -ne $Null}
$GroupsTable = @{}
$Groups.ForEach( {
$GroupsTable.Add([String]$_.SharePointSiteUrl, $_.SensitivityLabel) } )
Write-Host "Starting to update SharePoint sites with sensitivity labels..."
[int]$Updates = 0; [int]$NoUpdates = 0
ForEach ($Site in $SitesNoLabels) {
$Label = $Null
$Label = $GroupsTable.Item($Site.Url)
If ($Label) { # Update the site with the label we find
Write-Host ("Updating site {0} with label {1}" -f $Site.Url, $Label.Guid)
Set-SPOSite -Identity $Site.Url -SensitivityLabel $Label.Guid
$Updates++ }
Else {
Write-Host ("Can't find sensitivity label for site {0} - group might be deleted" -f $Site.Url)
$NoUpdates++ }
} #End ForEach Sites
Write-Host ("Finished Updating sites. {0} labels applied to sites. {1} sites could not be updated with labels" -f $Updates, $NoUpdates)
Attribution