Automating Version History Trim in All Document Libraries in a SharePoint Online Site Collection Using PowerShell
Managing version history in SharePoint Online is essential for maintaining an organized and efficient document library. By setting automatic version history trim, you can control the number of versions retained for each document, ensuring that your storage is used effectively and that users can easily access the most relevant versions. In this blog post, we'll walk you through the process of setting automatic version history trim on all document libraries in a SharePoint Online site collection using PowerShell.
Why Set Automatic Version History Trim?
Versioning in SharePoint Online allows you to track changes to documents and restore previous versions if needed. However, without proper management, versioning can lead to excessive storage consumption and cluttered document libraries. Setting automatic version history trim helps you:
- Optimize Storage: By limiting the number of versions, you can reduce storage costs and improve performance.
- Enhance Usability: Users can find the most relevant versions more easily without sifting through numerous outdated versions.
- Maintain Compliance: Ensure that your document management practices align with organizational policies and regulatory requirements.
Prerequisites
Before you begin, ensure that you have the following:
- SharePoint Online Management Shell: Download and install the latest version of the SharePoint Online Management Shell.
- Administrative Permissions: You must have SharePoint Administrator permissions to execute the PowerShell commands.
PowerShell Script to Set Automatic Version History Trim
Here's a PowerShell script to set automatic version history trim on all document libraries in a SharePoint Online site collection:
# Define variables
$SiteURL = "https://yourtenant.sharepoint.com/sites/yoursite"
$MajorVersionLimit = 500
$MinorVersionLimit = 20
$ExpireVersionsAfterDays = 30
# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
# Get all document libraries in the site collection
$Libraries = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 }
# Set versioning policy for each document library
foreach ($Library in $Libraries) {
Set-PnPList -Identity $Library -MajorVersions $MajorVersionLimit -MinorVersions $MinorVersionLimit
Set-PnPSiteVersionPolicy -Site $SiteURL -List $Library.Title -EnableAutoExpirationVersionTrim $true -ExpireVersionsAfterDays $ExpireVersionsAfterDays
}
Write-Host "Automatic version history trim set successfully on all document libraries."
Steps to Execute the Script
- Open SharePoint Online Management Shell: Launch the SharePoint Online Management Shell on your computer.
- Connect to SharePoint Online: Use the
Connect-PnPOnline
command to connect to your SharePoint Online site. Replacehttps://yourtenant.sharepoint.com/sites/yoursite
with the URL of your site collection. - Run the Script: Copy and paste the script into the SharePoint Online Management Shell and press Enter. The script will set the automatic version history trim on all document libraries in the specified site collection.
Conclusion
By using PowerShell to set automatic version history trim on all document libraries in a SharePoint Online site collection, you can efficiently manage your document versions, optimize storage, and maintain a clean and organized library. This approach ensures that your SharePoint environment remains scalable and user-friendly.
Feel free to customize the script to meet your specific requirements and share your experiences in the comments below!
Comments
Post a Comment