Forum Discussion

Fromelard's avatar
Fromelard
Steel Contributor
Feb 02, 2025

PowerShell Script to apply the retention Label in a full Site Collection

This script could help you to apply a Label in an entire Site Collection (All document libraries into all subsites).

It's using an old command for "Set-PnPLabel" (I was not able to apply it with the last version of this "command"). You will need to have the  PnP.PowerShell module version "1.12.0", this is why the start of the script is related to this component installation, you can remove it once your action is applied as expected.

# -----------------------------------------------------------------------------------
#COMPONENT INSTALLATION
#Get-InstalledModule -Name PnP.PowerShell 
#Find-Module -Name PnP.PowerShell -RequiredVersion 1.12.0
#Install-Module -Name PnP.PowerShell -RequiredVersion 1.12.0 -Force
#Get-InstalledModule -Name PnP.PowerShell 
# -----------------------------------------------------------------------------------
[string]$SiteCollectionRelativeURL = "MySiteCollection" 
[string]$LabeltoApply = "Auto-delete 15 years"

[string]$TeamSiteToUpdate = "https://MySPTenant.sharepoint.com/sites/$SiteCollectionRelativeURL/"
[string]$ListName = ""
# -----------------------------------------------------------------------------------
Import-Module PnP.PowerShell -DisableNameChecking
Connect-PnPOnline -Url $TeamSiteToUpdate -UseWebLogin
# -----------------------------------------------------------------------------------
#Get-PnPLabel 

$AllDocLibraries = Get-PnPList | Where-Object {$_.Hidden -eq $false}
if ($AllDocLibraries.Count -gt 0)
{
    foreach ($myDocLib in $AllDocLibraries)
    {
        $ListName = $myDocLib.Title
        $MyList = Get-PnPList -Identity $ListName
        $CurrentLabel = Get-PnPLabel -List $ListName  -ValuesOnly
        if ($CurrentLabel.TagName -ne $LabeltoApply)
        {
        Write-Host " >>>> LABEL ", $LabeltoApply , "- NEED TO BE APPLIED on DocLib", $ListName -ForegroundColor Red
        Set-PnPLabel -List $ListName -Label $LabeltoApply -SyncToItems $false
        }
        else
        {
        Write-Host " >>>> LABEL ", $LabeltoApply , "- Yet Applied on DocLib", $ListName -ForegroundColor Green
        }
        #Get-PnPLabel -List $ListName  -ValuesOnly
    }
}
$AllSubWebs = Get-PnPSubWeb -Recurse
if($AllSubWebs.count -gt 0)
{
    foreach ($mysubweb in $AllSubWebs)
    {
        Write-Host $mysubweb.Url -ForegroundColor Yellow
        Connect-PnPOnline -Url $mysubweb.Url -UseWebLogin
        $AllDocLibraries = Get-PnPList | Where-Object {$_.Hidden -eq $false}
        if ($AllDocLibraries.Count -gt 0)
        {
            foreach ($myDocLib in $AllDocLibraries)
            {
                $ListName = $myDocLib.Title
                $MyList = Get-PnPList -Identity $ListName
                $CurrentLabel = Get-PnPLabel -List $ListName  -ValuesOnly
                if ($CurrentLabel.TagName -ne $LabeltoApply)
                {
                    Write-Host " >>>> LABEL ", $LabeltoApply , "- NEED TO BE APPLIED on DocLib", $ListName -ForegroundColor Red
                    Set-PnPLabel -List $ListName -Label $LabeltoApply -SyncToItems $false
                }
                else
                {
                    Write-Host " >>>> LABEL ", $LabeltoApply , "- Yet Applied on DocLib", $ListName -ForegroundColor Green
                }
            }
        }
    }
}

This script was used to prevent the automatic cleanup implemented in the full tenant to documents older than 10 years.  I used it to apply this on site collections containing more than 200 subsites and 10 doc lib mini per subsites.

You can use it and adapt it as you need for your specific context.

Fab

No RepliesBe the first to reply

Resources