Skip to main content

Automating Version History Trim in All Document Libraries in a SharePoint Online Site Collection Using PowerShell

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:

  • PnP PowerShell module: Download and install the latest version of the SharePoint Online PnP PowerShell module.
  • 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 PnP PowerShell Shell: (requires PowerShell 7+)  Launch the SharePoint Online PnP PowerShell 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 PnP PowerShell 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 PnP 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...

TL;DR: How Copilot and Generative AI are Revolutionizing Technical Documentation

TL;DR: How Copilot and Generative AI are Revolutionizing Technical Documentation In the fast-paced world of IT, staying current with the latest technology developments is crucial. However, the sheer volume of technical documentation and white papers can be overwhelming. Enter Copilot and LLM-based generative AI, the game-changers that are transforming how we consume and retain information. The Challenge: Information Overload We've all been there—faced with a lengthy technical document or white paper that we know is important but simply don't have the time to read in full. The internet trope "TL;DR" (Too Long; Didn't Read) perfectly encapsulates this common dilemma. In an industry where time is money, the ability to quickly grasp the essence of complex documents is invaluable. The Solution: Copilot and Generative AI Copilot, powered by advanced generative AI models, offers a solution to this problem. By summarizing long technical documents and white papers, Copilot...