Archiving Old SharePoint Lists and Libraries using PnP PowerShell

Loading

Over time, SharePoint Online accumulates outdated lists and libraries that are no longer actively used but may still hold valuable historical data. Archiving these old lists and libraries helps optimize storage, improve performance, and maintain compliance. Using PnP PowerShell, you can automate the identification, backup, and archival of old SharePoint data.


Key Considerations for Archiving

Identify Old Lists and Libraries – Find content based on last modified date or last accessed date.
Export Data – Backup list and library items to CSV, JSON, or another SharePoint site.
Move to an Archive Site – Store archived lists/libraries in a dedicated SharePoint archive site.
Apply Retention Policies – Ensure compliance with data governance rules.
Delete or Hide – Reduce clutter by deleting or removing archived lists from active use.


Step 1: Connect to SharePoint Online

To manage SharePoint data, first connect to your SharePoint Online tenant.

$siteUrl = "https://yourtenant.sharepoint.com/sites/MainSite"
Connect-PnPOnline -Url $siteUrl -Interactive

✔ Ensures secure authentication before running commands.


Step 2: Identify Old Lists and Libraries

Find lists and libraries not modified in the last 2 years.

$cutoffDate = (Get-Date).AddYears(-2)
$lists = Get-PnPList | Where-Object { $_.LastItemModifiedDate -lt $cutoffDate }

Write-Host " Old Lists and Libraries (Not Modified Since $cutoffDate):"
$lists | Select Title, LastItemModifiedDate

✔ Identifies lists/libraries that haven’t been updated in over 2 years.


Step 3: Export List Data to CSV (Backup)

Backup list items to a CSV file for offline storage.

$exportPath = "C:\SharePoint_Archives"
if (!(Test-Path -Path $exportPath)) { New-Item -ItemType Directory -Path $exportPath }

foreach ($list in $lists) {
$items = Get-PnPListItem -List $list.Title
$items | Select-Object FieldValues | Export-Csv -Path "$exportPath\$($list.Title)_Backup.csv" -NoTypeInformation
Write-Host " Exported: $($list.Title) to $exportPath"
}

✔ Ensures a backup copy before archiving.


Step 4: Move Lists and Libraries to an Archive Site

Instead of deleting, move lists/libraries to a dedicated archive site.

$archiveSiteUrl = "https://yourtenant.sharepoint.com/sites/Archive"
Connect-PnPOnline -Url $archiveSiteUrl -Interactive

foreach ($list in $lists) {
Move-PnPList -Identity $list.Title -TargetWebUrl $archiveSiteUrl
Write-Host " Moved: $($list.Title) to Archive Site"
}

✔ Retains historical data while keeping the main site clean.


Step 5: Hide Archived Lists from Users

Prevent archived lists from appearing in site navigation.

foreach ($list in $lists) {
Set-PnPList -Identity $list.Title -Hidden $true
Write-Host " Hidden List: $($list.Title)"
}

✔ Ensures users do not accidentally use archived lists.


Step 6: Apply Retention Policy to Archived Content

Apply a 5-year retention policy to archived lists.

$labelName = "Archived Data Retention"
$retentionPeriod = 1825 # 5 years in days

New-PnPRetentionComplianceRule -Name $labelName -RetentionDuration $retentionPeriod -RetentionComplianceAction Keep
Write-Host " Retention Policy Applied: $labelName (5 Years)"

✔ Ensures archived content is retained for 5 years before deletion.


Step 7: Delete Old Lists (Optional Cleanup)

After archiving and backup, you can delete the old lists from the main site.

foreach ($list in $lists) {
Remove-PnPList -Identity $list.Title -Force
Write-Host " Deleted Old List: $($list.Title)"
}

✔ Cleans up unnecessary lists, reducing clutter and storage usage.


Step 8: Automate the Process (Scheduled Task)

To run this script monthly, create a scheduled task.

  1. Save the script as ArchiveLists.ps1.
  2. Open Task Scheduler → Create Basic Task.
  3. Set Trigger → Monthly.
  4. Action → Start a program → powershell.exe -File C:\Scripts\ArchiveLists.ps1.
  5. Ensure execution policy allows scripts using:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

✔ Keeps SharePoint clean without manual intervention.


Step 9: Generate an Archive Report

Export a list of archived libraries for tracking.

$archiveReport = @()

foreach ($list in $lists) {
$archiveReport += [PSCustomObject]@{
Title = $list.Title
LastModified = $list.LastItemModifiedDate
ArchivedOn = Get-Date
}
}

$archiveReport | Export-Csv -Path "C:\SharePoint_Archive_Report.csv" -NoTypeInformation
Write-Host " Archive Report Generated: C:\SharePoint_Archive_Report.csv"

✔ Provides a clear log of archived lists.

Leave a Reply

Your email address will not be published. Required fields are marked *