PowerShell Script to Extract Version History of SharePoint List Items

  1. # * * * * * * * Variables Section * * * * * * * * * * * * * * * * * * $WebURL = "http://<<SharePointsite>>/sites/Opportunity/"  
  2. $ListName = "Opportunity List"  
  3. $ReportFile = "F:\Opportunity_VersionHistory.csv"# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #delete file  
  4. if exists If(Test - Path $ReportFile) {  
  5.     Remove - Item $ReportFile  
  6. }#Get the Web and List  
  7. $Web = Get - SPWeb $WebURL  
  8. $List = $web.Lists.TryGetList($ListName)#Check  
  9. if list exists  
  10. if ($List - ne $null) {#Get all list items  
  11.     $ItemsColl = $List.Items#Write Report Header  
  12.     Add - Content - Path $ReportFile - Value "Item ID, Version Lable, Created by, Created at, Title, Comments"#Loop through each List item  
  13.     foreach($item in $ItemsColl) {#Iterate each version  
  14.         foreach($version in $item.Versions) {#Get the version content  
  15.             $VersionData = "$($item.id), $($version.VersionLabel), $($version.CreatedBy.User.DisplayName), $($version.Created),          $($version['Title']), $($version['Comments'])"  
  16.  
  17.             #Write to report  
  18.             Add - Content - Path $ReportFile - Value $VersionData  
  19.         }  
  20.     }  
  21. }  
  22. Write - Host "Version history has been exported successfully!"