<

Set\Update Version Count In SharePoint Document Library & Remove Older Versions

Published on
11,455 Points
5,355 Views
1 Endorsement
Last Modified:
Approved
In SharePoint, you have the ability to set the number of versions to retain through the settings of the specific document libraries.  I was recently faced with a challenge where a company implemented a new global policy which mandated only 10 versions be retained.  The policy had be implemented by notice of the collection administrators after users were sufficiently notified of the change so I it had to be something that was run

Unfortunately, with the first library they updated, they noticed when you changed the version retention number, the libraries did not automatically remove the older versions.  Only when you performed a future update of the document, did the older versions get trimmed off.  

After a little research, I couldn't find anything online which offered the solution I was looking for.  In the end I wrote a simple but very fast, effective, and powerful PowerShell script.

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
# 2 variables which need to be set
$siteCollection = "http://SharePoint/Sites/SiteCollection"
$documentRetentionCount=10

# get site collection
$site = new-object Microsoft.SharePoint.SPSite($siteCollection)

# loop through each subsite
foreach ($web in $site.AllWebs)
{
   write-host $web.url

   # loop through all lists in each subsite
   foreach ($list in $web.Lists)
   {
      # examine if BaseType of list is a Document Library and if versioning is turned on
      if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
      {
         # Set the Major version limit to keep the latest 10 versions
         $list.MajorVersionLimit = $documentRetentionCount
         $list.Update()
         foreach($item in $list.Items)
         {# Perform a system update on each item 
            $item.SystemUpdate($true)
         }
      }
   }
}
$web.Dispose();
$site.Dispose();

Open in new window


I've documented the code inline but I'm going to walk through it for clarity sake:

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
1.      First you have to make sure the snap-ins are loaded.  I've used this method for a long time because when I ran, then subsequently re-ran the script after making edits, it would say the snap-in was already loaded.  This way, it will check first, then only load it if necessary.

$siteCollection = "http://SharePoint/Sites/SiteCollection"
$documentRetentionCount=10
2.      The next block lists the two variables that are specific to a run.  My next version of this will be to set the web application and loop through all site collections, then subsites, and finally all document libraries.
foreach ($web in $site.AllWebs)
{
   write-host $web.url
3.      The first foreach loop loops through each subsite in the site collection, displaying the current subsite URL in the console window.
foreach ($list in $web.Lists)
{
   if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
4.      It then loops through each list looking only for Document Library type lists which have versioning enabled

5.      The script then sets the major version limit for the versioning-enabled document library to the specified variable and saves the updated property value to the list.

foreach($item in $list.Items)
{
$item.SystemUpdate($true)
}
6.      Now the important part in my situation was updating each item to remove any version greater than the one specified in the variable which actually removes the versions outside of the newly defined limit.

I know it's not rocket science but it saved an SP administrator time and effort of going through each library and setting the version number and updating each item to apply the setting.  Doing it manually would have also updated the "Modified Date" and "Modified By" fields for each document which would have also sent out alerts if there were any set.
1
Comment
Author:skipper68
0 Comments

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Join & Write a Comment

In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month