Entra / Microsoft 365 · Compliance & audit
Report priority cleanup spo audit records
Find Priority Cleanup audit records for SharePoint Online documents that policy tags and moves to the second-stage recycle bin.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-ExchangeOnline -ShowBanner:$false
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
param([int] $LookbackDays = 10,[string] $StartDate = (Get-Date).AddDays(-$LookbackDays),[string] $EndDate = "Get-Date")[array]$Modules = Get-Module | Select-Object NameIf ("ExchangeOnlineManagement" -notin $Modules.Name) {Write-Host "Connecting to Exchange Online..."Connect-ExchangeOnline -ShowBanner:$false}Write-Output "Scanning for Priority Cleanup audit records..."[array]$Records = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Formatted -SessionCommand ReturnLargeSet -ResultSize 5000 -Operations 'PriorityCleanupFileRecycled', 'PriorityCleanupTagApplied'$Records = $Records | Sort-Object Identity -UniqueIf ($Records.Count -eq 0) {Write-Host "No audit records found for Priority Cleanup operations"Break} Else {$Records = $records | Sort-Object Identity -Unique | Sort-Object { $_.CreationDate -as [datetime]} -DescendingWrite-Host ("Processing {0} audit records..." -f $Records.Count)}$Records | Group-Object Operations -NoElement | Sort-Object Count | Format-Table Name, Count -AutoSize# Report removed files$PriorityCleanupReport = [System.Collections.Generic.List[Object]]::new()ForEach ($Item in $Records) {$AuditData = $Item.AuditData | ConvertFrom-Json$ReportLine = [PSCustomObject][Ordered]@{TimeStamp = Get-Date ($AuditData.CreationTime) -format 'dd-MMM-yyyy HH:mm'User = $AuditData.UserIdAction = $AuditData.OperationItemType = $AuditData.ItemTypeSiteURL = $AuditData.SiteURLItem = $AuditData.SourceFileNameExtension = $AuditData.SourceFileExtensionObjectId = $AuditData.ObjectIdFileSizeBytes = $AuditData.FileSizeBytesLabelName = $AuditData.PolicyMatchInfo.LabelNameLabelID = $AuditData.PolicyMatchInfo.LabelId}$PriorityCleanupReport.Add($ReportLine)}$TaggedOperations = $PriorityCleanupReport | Where-Object { $_.Action -eq 'PriorityCleanupTagApplied' }$MovedOperations = $PriorityCleanupReport | Where-Object { $_.Action -eq 'PriorityCleanupFileRecycled' }Write-Host ""Write-Host ("Found {0} Priority Cleanup Tag Applied operations" -f $TaggedOperations.Count)$TaggedOperations | Group-Object SiteURL -NoElement | Sort-Object Count -Descending | Format-Table Name, CountWrite-Host ""Write-Host ("Found {0} Priority Cleanup File Recycled operations" -f $MovedOperations.Count)$MovedOperations | Group-Object SiteURL -NoElement | Sort-Object Count -Descending | Format-Table Name, Count# Export the report as an Excel worksheet or CSV fileIf (Get-Module ImportExcel -ListAvailable) {$ExcelGenerated = $trueImport-Module ImportExcel -ErrorAction SilentlyContinue$ExcelOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\PriorityCleanupReport.xlsx"If (Test-Path $ExcelOutputFile) {Remove-Item $ExcelOutputFile -ErrorAction SilentlyContinue}$PriorityCleanupReport | Export-Excel -Path $ExcelOutputFile -WorksheetName "Priority Cleanup" -Title ("Priority Cleanup Report {0}" -f (Get-Date -format 'dd-MMM-yyyy')) -TitleBold -TableName "PriorityCleanup"} Else {$CSVOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\PriorityCleanupReport.CSV"$PriorityCleanupReport | Export-Csv -Path $CSVOutputFile -NoTypeInformation -Encoding Utf8}If ($ExcelGenerated) {Write-Output ("Excel worksheet output written to {0}" -f $ExcelOutputFile)} Else {Write-Output ("CSV output file written to {0}" -f $CSVOutputFile)}Write-Output "All done - enjoy the Priority Cleanup data!"
Parameters
ParameterDefaultNotes
-LookbackDays10Number of days back to search the unified audit log.-StartDate(Get-Date).AddDays(-10)Start of the reporting window.-EndDateGet-DateEnd of the reporting window.Attribution
Author
Office365itpros