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...

Boosting Productivity with Microsoft 365 Copilot: A Guide for Experienced Microsoft 365 IT Professionals

  As seasoned Microsoft 365 consultants, leveraging cutting-edge tools to streamline workflows and enhance productivity is paramount. Microsoft 365 Copilot, an AI-powered assistant designed to augment Microsoft 365 applications, stands out as a revolutionary tool in this regard. Copilot integrates deeply within the Microsoft ecosystem, assisting with a range of tasks from drafting emails to developing complex Power Apps. In this blog post, we will explore the top five ways Microsoft 365 Copilot can increase productivity for experienced Microsoft 365 consultants, with a special focus on its capabilities in Power App development. 1. Streamlined Email and Document Drafting One of the most time-consuming tasks for information workers is drafting emails and documents. Microsoft 365 Copilot can significantly reduce the time spent on these activities through its intelligent suggestion capabilities. By analyzing the context and purpose of emails and documents, Copilot can suggest phras...