Delete items from preservation Hold library in bulk using pnp powershell

Опубликовано: 06 Июнь 2026
на канале: dev365
345
5

Delete items from preservation Hold library in bulk using pnp powershell
#office365 #official #office365training #m365 #microsoft

Powershell cmdLet-----------------------------------
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force

Ensure PnP PowerShell module is installed
if (-not (Get-Module -ListAvailable -Name PnP.PowerShell)) {
Write-Host "PnP.PowerShell module not found. Installing..." -ForegroundColor Yellow
Install-Module PnP.PowerShell -Scope CurrentUser -Force
Update-Module PnP.PowerShell -Scope CurrentUser -Force
}

Import the PnP PowerShell module
Import-Module PnP.PowerShell

$AppId = "1568f5bf-a0fe-43de-8856-f6bcb042b881" # Your registered app's Application ID

$UserOneDriveSite = "insert onedriveSiteURL here"

Write-Host "Connecting to OneDrive Site: $UserOneDriveSite" -ForegroundColor Cyan

Connect to User's OneDrive using PnP PowerShell
try {
Connect-PnPOnline -Url $UserOneDriveSite -Credentials (Get-Credential) -Verbose -ClientId $AppId
Write-Host "Successfully connected to the user's OneDrive." -ForegroundColor Green
} catch {
Write-Host "Failed to connect to the user's OneDrive. Exiting script." -ForegroundColor Red
exit
}

Check if 'Preservation Hold Library' exists and remove items
try {
$listItems = Get-PnPListItem -List 'Preservation Hold Library' -PageSize 500

if ($listItems.Count -eq 0) {
Write-Host "No items found in the Preservation Hold Library." -ForegroundColor Yellow
} else {
Write-Host "Removing items from the Preservation Hold Library..." -ForegroundColor Cyan

foreach ($item in $listItems) {
$Filename = $item.FieldValues.FileLeafRef
Write-Host "Removing $Filename..." -ForegroundColor Cyan
Remove-PnPListItem -List 'Preservation Hold Library' -Identity $item -Force
}

Write-Host "Items removed successfully from the Preservation Hold Library." -ForegroundColor Green
}
} catch {
Write-Host "Failed to remove items from the Preservation Hold Library. Exiting script." -ForegroundColor Red
exit
}

Disconnect from PnP session
Disconnect-PnPOnline

Write-Host "Preservation Hold Library cleanup complete!" -ForegroundColor Gree