PowerShell Script To Get SharePoint Document Versioning Report

Objective

This purpose of this document is to mention the steps to get a SharePoint Document Versioning report for a web application. The reusable script for this job has also attached with this document. This script offers -

  • To select the a web application to operate on
  • To Select the set of site collection to operate on
  • Generate a report in CSV format, which will be helpful for excel reporting.

Business Case

S. No. Business Case
1 Before we go ahead and start doing migration we need to extract many report and insights about the current SharePoint environment. One of those need is to get a SharePoint document versioning report in your SharePoint. This script will allow administrator to do this job easily.

Targeted Audience

  • SharePoint Application Developers
  • SharePoint Administrator
  • SharePoint Architect

Offerings

  • One reusable PowerShell script is provided which needs to be run to get SharePoint Versioning report.
  • Generate a report in excel format which includes information like:

      - Site Collection Title
      - Site Name
      - Library
      - File Name
      - File URL
      - File Type
      - Last Modified Date
      - Number of Versions
      - Latest Version Size
      - Version Size
      - Total size

Technical Details

Below are the technical details for this PowerShell script -

    Execution

    Prerequisite:

    Login to SharePoint Server as Farm Administrator and copy the required files as per your requirement.
    Run:

    1. Edit and Update Configurations.xml file as per your need. See below file.

    1. <?xml version="1.0" encoding="utf-8" ?>  
    2.   
    3. <Configuration Environment="Dev" Version="1.0.0.0">  
    4.     <GlobalWebApplications>  
    5.         <GlobalWebApplication url="http://mywebApplicationURL/">  
    6.             <SiteCollections>  
    7.                 <SiteCollection relativeURL="sites/SiteCollectionTitle" />  
    8.                 <SiteCollection relativeURL="sites/SiteCollectionTitle1" />  
    9.             </SiteCollections>  
    10.         </GlobalWebApplication>  
    11.     </GlobalWebApplications>  
    12. </Configuration>  
    2. Run the PowerShell Script as “Run as Administrator“.

    3. Browse the folder path where you have kept this PowerShell script file and execute a command.

    run

    Note: Copy and paste the above xml code in a notepad and save it as Configurations.xml.
PowerShell Script
  1. #check to see  
  2. if the PowerShell Snapin is added  
  3. if ((Get - PSSnapin | Where  
  4.     {  
  5.         $_.Name - eq "Microsoft.SharePoint.PowerShell"  
  6.     }) - eq $null)  
  7. {  
  8.     Add - PSSnapin Microsoft.SharePoint.PowerShell;  
  9. }  
  10. #SharePoint DLL  
  11.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
  12. $global: currentPhysicalPath = Split - Path((Get - Variable MyInvocation - Scope 0).Value).MyCommand.Path[xml] $xmlinput = (Get - Content "$global:currentPhysicalPath\Configurations.xml")  
  13. Function SPDocVersionSizeReport  
  14. {  
  15.     foreach($configWebApp in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication)  
  16.     {  
  17.         $webApp = Get - SPWebApplication $configWebApp.url - ErrorAction silentlycontinue  
  18.         if ($webApp - eq $null)  
  19.         {  
  20.             Write - host Web Application at url: $configWebApp.url does not Exists.. - foregroundcolor Red  
  21.         }  
  22.         else  
  23.         {  
  24.             try  
  25.             {  
  26.                 foreach($siteCollection in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.SiteCollections.SiteCollection)  
  27.                 {  
  28.                     $siteCollUrl = $($webApp.url + $siteCollection.relativeUrl)  
  29.                     $site = Get - SPSite - identity $siteCollUrl - ErrorAction SilentlyContinue  
  30.                     if ($site - ne $null)  
  31.                     {#Write the CSV Header - Tab Separated  
  32.                         $fileName = $site.RootWeb.Title + "_" + "VersionSizeReport" + ".csv"  
  33.                         "Site Collection Name `t Site Name`t Library `t File Name `t File URL `t File Type `t Last Modified `t No. of Versions `t Latest Version Size(MB) `t Versions Size(MB) `t Total File Size(MB)" | out - file $fileName  
  34.                         # Fill out the names of those document libraries that should be excluded below  
  35.                         $forbidden = @("Pages""Converted Forms""Master Page Gallery""Customized Reports""Documents",  
  36.                             "Form Templates""Images""List Template Gallery""Theme Gallery""Reporting Templates",  
  37.                             "Site Collection Documents""Site Collection Images""Site Pages""Solution Gallery",  
  38.                             "Style Library""Web Part Gallery""Site Assets""wfpub")  
  39.                         foreach($web in $site.AllWebs)  
  40.                         {#Loop through each List  
  41.                             foreach($List in $Web.Lists)  
  42.                             {#Get only Document Libraries & Exclude Hidden System libraries  
  43.                                 if (($List.BaseType - eq "DocumentLibrary") - and($List.Hidden - eq $false) - and($SystemLists - notcontains $List.Title))  
  44.                                 {  
  45.                                     foreach($ListItem in $List.Items)  
  46.                                     {##Consider items with 5 + versions And apply Date Filter  
  47.                                         if (($ListItem.Versions.Count - gt 1) - and($ListItem['Modified'] - gt([DateTime]::Now.AddYears(-1))))  
  48.                                         {  
  49.                                             $versionSize = 0# File Versioning details  
  50.                                             foreach($FileVersion in $ListItem.File.Versions)  
  51.                                             {  
  52.                                                 $versionSize = $versionSize + $FileVersion.Size;  
  53.                                             }#Calculate Total File Size in MB  
  54.                                             $ToalFileSize = [Math]::Round(((($ListItem.File.Length + $versionSize) / 1024) / 1024), 2)  
  55.                                             $VersionSize = [Math]::Round((($versionSize / 1024) / 1024), 2)  
  56.                                             # Size of the current version  
  57.                                             $CurrentVersionSize = [Math]::Round((($ListItem.File.Length / 1024) / 1024), 2)  
  58.                                             # Log the data to a CSV  
  59.                                             if ($versionSize - gt 0)  
  60.                                             {  
  61.                                                 "$($Site.RootWeb.Title) `t $($Web.Title) `t $($List.Title) `t $($ListItem.Name) `t $($Web.Url)/$($ListItem.Url) `t $($ListItem['File Type'].ToString()) `t $($ListItem['Modified'].ToString())`t $($ListItem.Versions.Count) `t $CurrentVersionSize `t $($versionSize) `t $($ToalFileSize)" | Out - File $fileName - Append  
  62.                                             }  
  63.                                         }  
  64.                                     }  
  65.                                 }  
  66.                             }  
  67.                             $Web.Dispose()  
  68.                         }  
  69.                         $site.Dispose()  
  70.                     }  
  71.                     else  
  72.                     {  
  73.                         Write - host - ForeGroundColor Red "Site Collection with this name does not exist. Please check if you have typed the URL correctly."  
  74.                     }  
  75.                 }#Message to console  
  76.                 write - host "Versioning Report Generated Successfully!"  
  77.             }  
  78.             catch [Exception]  
  79.             {  
  80.                 write - output(”Error: ”+$_.Exception.ToString())  
  81.             }  
  82.         }  
  83.     }  
  84. }  
  85. #Call the Function to Generate Version History Report  
  86. SPDocVersionSizeReport $xmlinput  
Output

This script will generate a separate report file for every site collection it operates on. See below.

Output

 

Up Next
    Ebook Download
    View all
    Learn
    View all