PowerShell: Finding and Deleting Files in SharePoint Libraries
Introduction
Managing files in SharePoint libraries can be challenging, especially when dealing with test data or bulk operations. For instance, you may need to find and delete files containing specific names or patterns to reset a testing environment. This blog post demonstrates how to use PowerShell to locate and delete files in SharePoint libraries, including associated data in lists, to streamline the testing process.
In this example, we had PDF files containing a series of receipts that were split into individual files using Power Automate with the Encodian connector. For testing purposes, we needed to bulk delete these files and their associated data to restart the process with the same dataset. The provided PowerShell script automates this task.
PowerShell Script to Find and Delete Files
The following PowerShell script identifies files in a SharePoint library based on specific naming patterns and deletes them along with their associated data in lists.
Script Overview
- Purpose: Locate files in a SharePoint library, delete them, and remove associated data from lists using the ID of the receipt file which is saved within the list to know where the data comes from.
- Key Features:
- Connects to both source and destination SharePoint sites.
- Finds files based on naming patterns.
- Deletes files and associated list items.
- Logs all operations for auditing purposes.
PowerShell Script
#List Parameters
$receiptsList = "Receipts"
$receiptsLibrary = "Receipt Documents"
function Delete-Data {
param (
[Parameter(Mandatory = $true)
][string] $SourceSiteUrl,
[Parameter(Mandatory = $true)]
[string]$SourceFolderPath,
[Parameter(Mandatory = $true)]
[string]$DestinationSiteUrl,
[Parameter(Mandatory = $true)]
[string]$DestinationFolderPath
)
# Generate a unique log file name using today's date and time
$todayDate = Get-Date -Format "yyyy-MM-dd_HHmm"
$informationLogFileName = "copy-file-information_$todayDate.log"
$informationLogFilePath = Join-Path -Path $PSScriptRoot -ChildPath $informationLogFileName
# Tenant URL
$tenantUrl = "https://contoso.sharepoint.com"
$startTime = Get-Date -Format "yyyy-MM-dd HH:mm"
$informationMessage = "Delete Process Started '$startTime'."
Write-Host $informationMessage -ForegroundColor Green
Add-Content -Path $informationLogFilePath -Value $informationMessage
# Connect to the source and destination SharePoint sites
Connect-PnPOnline -Url $SourceSiteUrl -Interactive
$sourceConn = Get-PnPConnection
Connect-PnPOnline -Url $DestinationSiteUrl -Interactive
$destinationConn = Get-PnPConnection
$sourceRelativeFolerPath = $SourceSiteUrl.replace($tenantUrl, "") + $SourceFolderPath
$destinationRelativeFolderPath = $DestinationSiteUrl.replace($tenantUrl, "") + $DestinationFolderPath
#Get Source Data
# Get source files
$sourceFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $SourceFolderPath -ItemType File -Recursive -Connection $sourceConn
$informationMessage = "Total '$($sourceFiles.count)' files."
Write-Host $informationMessage -ForegroundColor Green
Add-Content -Path $informationLogFilePath -Value $informationMessage
foreach ($file in $sourceFiles) {
# Get sub folder Path
$subFolderPath = $file.ServerRelativeUrl.Replace($sourceRelativeFolerPath, "").replace($file.Name, "")
# Get destination relative folder path with sub folder
$newDestinationRelativeFolderPath = $destinationRelativeFolderPath + $subFolderPath.TrimEnd("/")
try {
#check where file exists before deleting file
$fileUrl = $newDestinationRelativeFolderPath + "/" + $file.Name
$fileAslistItem = Get-PnPFile -Url $fileUrl -AsListItem -Connection $destinationConn
if($fileAslistItem)
{
#Get receipts Docs from remittance docs library using $file.Name
$recFiles = Find-PnPFile -List $receiptsLibrary -Match "*$($file.Name)" -Connection $destinationConn
$recFiles | ForEach-Object {
$remFileAsItemId = (Get-PnPFile -Url $_.ServerRelativeUrl -AsListItem -Connection $destinationConn).Id
#Get/Delete receipts Data from receipts lists
Get-PnPListItem -List $receiptsList -PageSize 5000 -Connection $destinationConn| Where-Object {$_.FieldValues.DocumentSourceReference -eq $remFileAsItemId }| ForEach-Object{
Remove-PnPListItem -List $receiptsList -Identity $_.Id -Connection $destinationConn -Force
$informationMessage = "Deleting Remittance ID $($_.Id) for sourceref $($remFileAsItemId)"
Write-Host $informationMessage -ForegroundColor Green
Add-Content -Path $informationLogFilePath -Value $informationMessage
}
#delete receipt doc
Remove-PnPListItem -List $receiptsLibrary -Identity $remFileAsItemId -Connection $destinationConn -Force
$informationMessage = "Deleting rem document ID $($remFileAsItemId)"
Write-Host $informationMessage -ForegroundColor Green
Add-Content -Path $informationLogFilePath -Value $informationMessage
}
#delete temp doc
Remove-PnPFile -ServerRelativeUrl $fileUrl -Connection $destinationConn -Force
$informationMessage = "Deleting temp library file $($fileUrl)"
Write-Host $informationMessage -ForegroundColor Green
Add-Content -Path $informationLogFilePath -Value $informationMessage
}
else{
$informationMessage = "File '$($file.Name)' does not exist in '$newDestinationRelativeFolderPath'"
Add-Content -Path $informationLogFilePath -Value $informationMessage
}
}
catch {
$errorMessage = "Error: Deleting file '$($file.Name)' to '$newDestinationRelativeFolderPath' - $($_.Exception.Message)"
Write-Host $errorMessage -ForegroundColor Red
Add-Content -Path $errorLogFilePath -Value $errorMessage
}
}
}
Delete-Data `
-SourceSiteUrl "https://contoso.sharepoint.com/teams/t-app" `
-SourceFolderPath "/Test Data/UAT Testing/Apr 2025/Temp Library/ms/Contoso" `
-DestinationSiteUrl "https://contoso.sharepoint.com/teams/u-app" `
-DestinationFolderPath "/TempLibrary/ms/Contoso" `
The script deletes all the files and corresponding extracting data using document intelligence capability within a logic app.
Conclusion
Using this PowerShell script, you can efficiently locate and delete files in SharePoint libraries, along with their associated data in lists. This is particularly useful for resetting test environments or managing large datasets. By automating these tasks, you can save time and ensure consistency in your SharePoint environment.