Entra / Microsoft 365 · Compliance & audit
Audit retention label application records
Searches audit records for retention label applications and reports labels applied by auto-label policies.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-ExchangeOnline
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
param([int] $LookbackDays = 14,[datetime] $StartDate = (Get-Date).AddDays(-14),[datetime] $EndDate = (Get-Date).AddDays(1))[array]$Modules = Get-Module | Select-Object -ExpandProperty NameIf ("ExchangeOnlineManagement" -notin $Modules) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"break}# Retention label to check for - change this to your preferred label name$RetentionLabel = "Teams recordings"$TotalSeconds = 0# Start and end date for the audit scan. By default, we look for 14 days, but you can choose any value you like up to 365 (assuming Office 365 E5)$OutputCSVFile = "C:\temp\TaggedTeamsRecordings.csv"# Find the audit records[array]$Records = Search-UnifiedAuditLog -Operations TagApplied -StartDate $StartDate -EndDate $EndDate -Formatted -ResultSize 5000 -SessionCommand ReturnLargeSetIf (!$Records) {Write-Host "No audit records found - exiting!"; break}# Sort to remove duplicate audit records$Records = $Records | Sort-Object Identity -Unique# Check the audit records for those for auto-label policies which applied the retention label we're interested in$TaggedRecordings = [System.Collections.Generic.List[Object]]::new()ForEach ($Rec in $Records) {$AuditData = $Rec.AuditData | ConvertFrom-JsonIf (($AuditData.DestinationLabel -eq $RetentionLabel) -and ($AuditData.UserType -eq "CustomPolicy")) {$RecordingFileName = $AuditData.DestinationFileName$DateLoc = ($RecordingFileName.IndexOf("-202")+1)$RDate = $RecordingFileName.SubString($DateLoc,8)$TimeLoc = $DateLoc + 9$RTime = $RecordingFileName.SubString($TimeLoc,4)$RecordingDateTime = $RDate + $RTime[datetime]$RecordingDate = [datetime]::ParseExact($RecordingDateTime,"yyyyMMddHHmm",$null)[datetime]$TaggingDate = Get-Date($AuditData.CreationTime)$TimeToTag = ($TaggingDate - $RecordingDate)$TotalSeconds = $TotalSeconds + $TimeToTag.TotalSeconds$TimeToTagFormatted = "{0:dd}d:{0:hh}h:{0:mm}m" -f $TimeToTag# Add the data about our record$DataLine = [PSCustomObject] @{Workload = $AuditData.WorkloadRecording = $AuditData.DestinationFileName"Retention Label" = $AuditData.DestinationLabel"Tagging Date" = Get-Date($AuditData.CreationTime) -format 'dd-MMM-yyyy HH:mm:ss'"Recording date" = Get-Date($RecordingDate) -format 'dd-MMM-yyy HH:mm:ss'"Days to label" = $TimeToTagFormattedSite = $AuditData.SiteURLFullURL = $AuditData.ObjectId }$TaggedRecordings.Add($DataLine)} # End if} # End ForEachClear-Host# All done$TaggedRecordings | Export-CSV -NoTypeInformation $OutputCSVFileWrite-Host ("{0} audit records found for auto-applying the {1} retention label between {2} and {3}" -f $TaggedRecordings.Count, $RetentionLabel, $StartDate, $EndDate)$AverageSeconds = $TotalSeconds/$TaggedRecordings.Count$AverageTimeToTag = [timespan]::fromseconds($AverageSeconds)$AverageTimeToTagFormatted = "{0:dd}d:{0:hh}h:{0:mm}m" -f $AverageTimeToTagWrite-Host ("Average elapsed time to auto-label recordings: {0}" -f $AverageTimeToTagFormatted)Write-Host ("The report file is available in {0}." -f $OutputCSVFile)
Parameters
ParameterDefaultNotes
-LookbackDays14How many days back to search unified audit log records.-StartDate(Get-Date).AddDays(-14)Start of the audit log search window.-EndDate(Get-Date).AddDays(1)End of the audit log search window.Attribution
Author
Office365itpros