Something is modifying my Inactive SharePoint Sites

In the SharePoint Admin Centre I often use the Last Activity date to identify if sites have been accessed recently before deciding on whether to archive the site or not. Unfortunately if you have a third-party backup solution you may have an issue.

I noticed this issue does occur event though backup software shouldn’t change the Activity date. This makes it more difficult to identify sites that might be candidates for archiving without a lot more work.

PowerShell to the rescue! This script looks at the document libraries in a site and returns the last modified date on each library.

# Parameters
$siteUrl = "https://tenantname.sharepoint.com/sites/sitename" # Replace with your SharePoint site URL

# Connect to SharePoint site
Connect-PnPOnline -Url $siteUrl -ClientID xxxxxxx-xxxx-xxxx-xxxx-xxxxxxx -Interactive

# Get all lists and filter for document libraries
$libraries = Get-PnPList | Where-Object { $_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $FALSE }

# Output the results
Write-Host "Document Libraries in $siteUrl" -ForegroundColor Green
Write-Host "-----------------------------------" -ForegroundColor Green

foreach ($library in $libraries) {
    # Get the last modified date of the library
    $lastModified = $library.LastItemUserModifiedDate
    
    Write-Host "Library: $($library.Title)" + "Last Modified: $lastModified"
}

Beware, this approach isn’t a silver bullet. You can’t tell if the site was accessed by other users. This script will only tell you the last time a document library was updated e.g. new file added or existing file modified.

If the Site is associated with a Microsoft Team then you may also find the Team is used for conversations or other purposes. It is common to find Teams without any document content in SharePoint.

Other potential approach is to look at Audit log data and determine if the last access of the site that isn’t the backup software. The Search-UnifiedAuditLog PowerShell commandlet can be used to access the Audit Logs. This script can be used to filter audit logs for SharePoint activity.


# Connect to SharePoint Online
Connect-SPOService -Url https://yourtenant-admin.sharepoint.com

# Get audit logs excluding the backup account
$auditLogs = Search-UnifiedAuditLog -StartDate (Get-Date).AddDays(-30) -EndDate (Get-Date) -RecordType SharePointFileOperation -Operations FileAccessed -ResultSize 5000
$filteredLogs = $auditLogs | Where-Object { $_.UserId -ne "backupaccount@yourdomain.com" }

# Display the last accessed date by users excluding the backup account
$filteredLogs | Select-Object UserId, CreationDate | Sort-Object CreationDate -Descending | Select-Object -First 1


Discover more from SharePoint Moments

Subscribe to get the latest posts sent to your email.

Leave a comment