Skip to main content

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

  1. Open SharePoint Online Management Shell: Launch the SharePoint Online Management Shell on your computer.
  2. Connect to SharePoint Online: Use the Connect-PnPOnline command to connect to your SharePoint Online site. Replace https://yourtenant.sharepoint.com/sites/yoursite with the URL of your site collection.
  3. 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

Popular posts from this blog

SharePoint 2013 - Event ID: 8319

EventID: 8319 This is the error and this is the EVENT NAME Table  SqlIo Data _Partition27 has 443957248 bytes that have exceeded the max bytes 442857142 Which is the SQL IO Usage event To know the database name Go to Central Admin  First, we must get the logging database name The database name is in this example "WSS_Logging" Now we need to find which table is taking most of the space inside the WSS logging Database. You can check from the  SQL Server Login to  SQL Server  Management Studio -> Select your logging Database (Right Click) -> Reports- > Standard Reports -> Disk Usage by Top Tables The SQL IO Usage event We can reduce the retention period of any events (default is 14) Table SqlIoData_Partition27 has 443957248 bytes that has exceeded the max bytes 442857142 In my case its SQL IO Any way if we couldn’t know exactly which identity is causing the problem we can just Set them all to any number o...

Forget SharePoint Adoption; Focus on Utilization!

SharePoint Adoption     Many SharePoint deployments stall or are never implemented throughout an organization.  These less than desired results are often use the excuse of a lack of SharePoint "adoption". Problems with "Adoption"      The whole idea of SharePoint "Adoption" implies a passive approach to rolling out a SharePoint deployment and a style of "build it and they will come" philosophy.  Generally, users, especially non-technical users, continue to do what they've always done out of habit or previous training.  Users will ONLY change their day-to-day tasks or tools if they are told to do so by their boss or if they're convinced a new tool or method can save them time or solve some obvious problem, such as daily tasks. Active Approach to drive Utilization      Any successful SharePoint deployment has to be proactively driven on a based on providing real and current solutions.  SharePoint deployments s...