Amarender Peddamalku is a consultant at CapTech focused on designing and developing SharePoint portal applications along with the Business Intelligence solutions across the private, governmental, and non-governmental sectors.
SharePoint 2010 PowerShell Script to extract the version history
May 31 2012
Did you ever wanted to get a report on version history stored in SharePoint (list and/or library)! If so you are at the right place. Content management made easier by SharePoint! SharePoint 2010 includes a great feature called Version Control by providing one centralized location for files. Version control provides the functionality to save multiple instances (versions) of the same document and you can easily configure versioning control for a document library by going to Library Settings > General Settings > Versioning Settings. You can also set up retention policies, check documents in and out, create a document version history (major/minor) that you can access, manage, and modify easily with your web browser.
Recently one of the clients asked me to provide the list of documents that are modified by a particular user (Let’s say UserXYZ) in a document library. Sounds pretty simple, right? The first thought that came to my mind was to
- Create a view in the document library
- Set up a filter on the view where “Modified By” is equal to UserXYZ as shown in the Figure 1
- Export the view to Excel
Figure 1: Setting up a filter in the SharePoint views
But I was quick to realize that it’s not the right solution. Out-of-Box SharePoint document library/list views can only filter items based on the most recent version of file/document! Out-of-box views can’t iterate through the previous versions of the file. What a limitation!
Let’s look at the following scenario where we have a document in a document library (versioning enabled) which is modified by UserXYZ. Say after sometime another user called UserABC came in and modified the same document. So now we have 2 versions of the same document with the most recent version being modified by UserABC. Now I would like to have the list/report of documents that are modified by UserXYZ. If I create view with “Modified By” is equal to UserXYZ (refer to Figure 1), the view doesn’t return any items because Out-of-box views can’t look into the version history of the item/file. So even though UserXYZ modified the document, it would never show up in the view because it’s not the most recent version. Alright...What to do now, call Microsoft? Just kidding! Here comes the hulk called PowerShell to rescue us! Hulk? Yeah.. It’s really powerful, I love it.
So I’ve decided to write a PowerShell Script to iterate through all the versions of all the items in a given document library or a list. The following PowerShell script would provide you the list of items which are modified by UserXYZ.
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$SPWeb = Get-SPWeb http://sp2010/sites/test/ #Change the SharePoint site name here#
$List = $SPWeb.Lists["Documents"] #Change the document library name here#
$ItemsColl = $List.Items
Write-Host "************************************************************" -ForegroundColor Green
Write-Host "The following report provides you the list of documents that are modified by UserXYZ" -ForegroundColor Green
Write-Host "------------------------------------------------------------" -ForegroundColor Green
foreach ($item in $ItemsColl)
{
foreach($Ver in $item.Versions)
{
if ($Ver.CreatedBy.User.DisplayName -eq "UserXYZ") #Change the user name here#
{
Write-Host $item.Name "'s version" $Ver.VersionLabel "has been Modified by" $Ver.CreatedBy.User.DisplayName ":: URL ->" $item.Url -ForegroundColor Green
}
}
}
Write-Host "------------------------------------------------------------" -ForegroundColor Green
Write-Host "************************************************************" -ForegroundColor Green
$SPWeb.Dispose()Remember to change SharePoint site name, Document library name and user name according to your environment setup. Once you run the above script you would see output/result in the following format.
******************************************************************** The following report provides you the list of documents that are modified by UserXYZ --------------------------------------------------------------------- Sampledoc.txt 's version 2.4 has been Modified by UserXYZ :: URL -> Documents/ Sampledoc.txt testdoc.txt 's version 0.2 has been Modified by UserXYZ :: URL -> Documents/ testdoc.txt --------------------------------------------------------------------- ********************************************************************
Wasn’t that cool! OK, what if you are required to get the list of documents which are modified by UserXYZ in an entire site? Let’s make it happen by tweaking the script to make it more powerful. By the way I am bored of using UserXYZ. To add some more flavor to our script let’s create a report which provides you the list of documents which are modified by System Account. This kind of requirement may surface during the migration from MOSS 2007 to SharePoint 2010 or when you move documents from one environment to other. If you don’t have the same user in both the environments, some of the documents may show the “Modified By” as System Account.
The following PowerShell Script would iterate through each item’s version history of all the lists and document libraries in a given SharePoint site to identify the documents that are modified by System Account.
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
Write-Host "***********************************************************************"
Write-Host "***** The following report provides you the list of documents that are modified by System Account ***" -ForegroundColor Green
Write-Host "***********************************************************************"
$SPWeb = Get-SPWeb http://sp2010/sites/PShell/ #Change the SharePoint site name here#
foreach($List in $SPWeb.Lists)
{
$ItemsColl = $List.Items
foreach ($item in $ItemsColl)
{
foreach($prevVer in $item.Versions)
{
if ($prevVer.CreatedBy.User.DisplayName -eq "System Account")
{
Write-Host $item.Name "'s version" $prevVer.VersionLabel ";" "URL-->" $item.Url -ForegroundColor Green
}
}
}
}
Write-Host "******************* DONE **********************"
$SPWeb.Dispose()In recent times I’ve received lot of requests asking for a way to download the version history of SharePoint items in a given document library or list (which is shown in the Version history dialog box for each item through SharePoint User interface).To get there modify one of the above scripts by replacing
if($Ver.CreatedBy.User.DisplayName -eq "UserXYZ") #Change the user name here#
{
Write-Host $item.Name "'s version" $Ver.VersionLabel "has been Modified by" $Ver.CreatedBy.User.DisplayName ":: URL ->" $item.Url -ForegroundColor Green
}With the following
Write-Host $item.Name ":: version:" $Ver.VersionLabel ":: Modified by:" $Ver.CreatedBy.User.DisplayName ":: URL ->" $item.Url -ForegroundColor Green
Also you can export the output of PoweShell script to Excel by using Export-Csv Cmdlet.You should be good to go. That’s it for now and I hope you’ve enjoyed the blog post! Feel free to email me if you have any questions.
You can download the attached file to access PoweShell Scripts.
| Attachment | Size |
|---|---|
| PowerShell Scripts Version history report.zip | 1.42 KB |
© 2012 CapTech Ventures, Inc. All Rights Reserved. Legal Notices.