Report and Delete Preservation Hold Library
When you enable and disable Retention Policies in Microsoft 365 these policies will not always be cleared from SharePoint sites. This means that the Preservation Hold Library cannot be deleted.
To check for invalid Retention Policies, use this Microsoft Support Self-service link: https://aka.ms/PillarInvalidRetention
Create a report of all sites use the following script:
$AdminUrl = "tenantname-admin.sharepoint.com"
$AppID = "887efc0e-2b88-4f37-f510-85ffcfkk20l6"
$CertificateThumbPrint = "08834534738C896A45194F6854DD45E0940WE54D"
$Organization = "tenantame.onmicrosoft.com"
$TenantURL = "https://tenantname.sharepoint.com"
Connect-PnPOnline -Url $AdminUrl -ClientId $AppID -Thumbprint $CertificateThumbPrint -Tenant $Organization
# Path to the CSV file
$CsvFilePath = "C:\sites.csv"
# Import the CSV file
$Sites = Import-Csv -Path $CsvFilePath
# Connect to PnP Online
try {
Connect-PnPOnline -Url $AdminUrl -ClientId $AppID -Thumbprint $CertificateThumbPrint -Tenant $Organization
} catch {
Write-Host -ForegroundColor Red "Failed to connect to PnP Online: $_"
exit
}
# Loop through each site name
foreach ($Site in $Sites) {
$SiteURL = "$TenantURL/sites/$($Site.sitename)"
$LibraryName = "Preservation Hold Library"
# Connect to site
try {
Connect-PnPOnline -Url $SiteURL -ClientId $AppID -Thumbprint $CertificateThumbPrint -Tenant $Organization
} catch {
Write-Host -ForegroundColor Red "Failed to connect to site: $SiteURL - $_"
continue
}
# Get the Library
$Library = Get-PnPList -Identity $LibraryName -ErrorAction SilentlyContinue
# Check if document library exists
If ($Library -ne $Null) {
# Set Allow Deletion Property to True
$Library.AllowDeletion = $True
$Library.Update()
Invoke-PnPQuery
# PowerShell to remove document library
Remove-PnPList -Identity $LibraryName -Force -Recycle
Write-Host -ForegroundColor Green "Preservation Hold Library Deleted Successfully for site: $SiteURL"
} else {
Write-Host -ForegroundColor Yellow "Could not find Preservation Hold Library for site: $SiteURL"
}
}
Use the CSV-file to create a list of all SharePoint sites:
SiteURL
https://tenantname.sharepoint.com/teams/External
https://tenantname.sharepoint.com/teams/ICT
https://tenantname.sharepoint.com/Sites/BHO
https://tenantname.sharepoint.com/Sites/Releases
https://tenantname.sharepoint.com/teams/Management
To delete all Preservation Hold Libraries of all sites from the export file, use the follwing script:
$AdminUrl = "tenantname-admin.sharepoint.com"
$AppID = "887efc0e-2b88-4f37-f510-85ffcfkk20l6"
$CertificateThumbPrint = "08834534738C896A45194F6854DD45E0940WE54D"
$Organization = "tenantame.onmicrosoft.com"
Connect-PnPOnline -Url $AdminUrl -ClientId $AppID -Thumbprint $CertificateThumbPrint -Tenant $Organization
# Path to the CSV file
$CsvFilePath = "C:\sites.csv"
# Import the CSV file
$Sites = Import-Csv -Path $CsvFilePath
# Connect to PnP Online
try {
Connect-PnPOnline -Url $AdminUrl -ClientId $AppID -Thumbprint $CertificateThumbPrint -Tenant $Organization
} catch {
Write-Host -ForegroundColor Red "Failed to connect to PnP Online: $_"
exit
}
# Loop through each site name
foreach ($Site in $Sites) {
$SiteURL = $($Site.SiteURL)
$LibraryName = "Preservation Hold Library"
# Connect to site
try {
Connect-PnPOnline -Url $SiteURL -ClientId $AppID -Thumbprint $CertificateThumbPrint -Tenant $Organization
} catch {
Write-Host -ForegroundColor Red "Failed to connect to site: $SiteURL - $_"
continue
}
# Get the Library
$Library = Get-PnPList -Identity $LibraryName -ErrorAction SilentlyContinue
# Check if document library exists
If ($Library -ne $Null) {
# Set Allow Deletion Property to True
$Library.AllowDeletion = $True
$Library.Update()
Invoke-PnPQuery
# PowerShell to remove document library
Remove-PnPList -Identity $LibraryName -Force -Recycle
Write-Host -ForegroundColor Green "Preservation Hold Library Deleted Successfully for site: $SiteURL"
} else {
Write-Host -ForegroundColor Yellow "Could not find Preservation Hold Library for site: $SiteURL"
}
}
Share this content:
Geef een reactie