Versioning in SharePoint is an awesome feature that lets users create versions of their documents. However, when left unchecked, especially when the below category of “Document Version History” is selected – an end user will be able to create practically an indefinite number of versions. This is not a recommended best practice and may end up using a huge amount of storage over the years.

SharePoint

While you can always enable versioning settings, and delete the older versions manually, this will be a very cumbersome task, for libraries/lists with more than 1000s of list items.

The script below takes care of that! This script will be helpful in identifying the list of the versions, which needs to be deleted, based on dates of creation, and other input parameters.

Modes and Usage of the Script

This script has three modes,

However, it is to be noted that the current version of the document (Current Major Version and current Minor Version) will not be deleted, even if it falls into the category. This script will also let us users know of the storage size saved by means of deletion of the versions.

SharePoint

Input Parameters

The script takes in three input parameters, with sample examples are,

$myWeb"http://mysharepoint.com/sites/myweb1"The URL of the web or subsite where the list/library is located
$myList"Fantastic Library"The name of the list/library
$myDate"2014-01-01"The date beyond each which the versions are to be deleted
$myPath "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv" The location and name of the CSV report

Code for Delete item versions in SharePoint using PowerShell – Report Mode

  1. ###########################################################################
  2. #### Delete-Item-Versions-in-SharePoint using PowerShell - Report Mode ####
  3. ###########################################################################
  4. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
  5. {
  6. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  7. }
  8. $myWeb = "http://mysharepoint.com/sites/myweb1"
  9. $myList = "Fantastic Library"
  10. $myDate = "2014-01-01"
  11. $myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"
  12. $results = @()
  13. $storageSaved = 0
  14. $SPWeb = Get-SPWeb $myWeb
  15. foreach ($list in $SPWeb.Lists)
  16. {
  17. if ($list.Title -eq $myList)
  18. {
  19. Write-Host "Name: " $list.Title
  20. Write-Host "Type: " $list.BaseType
  21. $itemColl = $list.Items
  22. foreach ($item in $itemColl)
  23. {
  24. foreach ($ver in $item.Versions)
  25. {
  26. #this block for non-current versions
  27. if (!$ver.IsCurrentVersion)
  28. {
  29. if ((Get-Date $ver.Created) -lt (Get-Date $myDate))
  30. {
  31. $RowDetails = @{
  32. "Document Name" = $item.Name
  33. "Version Number" = $ver.VersionLabel
  34. "Created Date" = $ver.Created
  35. "Created By" = $ver.CreatedBy
  36. "Delete Flag" = "Can be deleted"
  37. "Date Flag" = "Match"
  38. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  39. }
  40. $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)
  41. $storageSaved = $storageSaved + $tempSize
  42. $results += New-Object PSObject -Property $RowDetails
  43. }
  44. else
  45. {
  46. $RowDetails = @{
  47. "Document Name" = $item.Name
  48. "Version Number" = $ver.VersionLabel
  49. "Created Date" = $ver.Created
  50. "Created By" = $ver.CreatedBy
  51. "Delete Flag" = "Do not Delete"
  52. "Date Flag" = "Does not Match"
  53. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  54. }
  55. $results += New-Object PSObject -Property $RowDetails
  56. }
  57. }
  58. #this block for current versions
  59. else
  60. {
  61. if ((Get-Date $ver.Created) -lt (Get-Date $myDate))
  62. {
  63. $RowDetails = @{
  64. "Document Name" = $item.Name
  65. "Version Number" = $ver.VersionLabel
  66. "Created Date" = $ver.Created
  67. "Created By" = $ver.CreatedBy
  68. "Delete Flag" = "Do not Delete "
  69. "Date Flag" = "Match"
  70. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  71. }
  72. $results += New-Object PSObject -Property $RowDetails
  73. }
  74. else
  75. {
  76. $RowDetails = @{
  77. "Document Name" = $item.Name
  78. "Version Number" = $ver.VersionLabel
  79. "Created Date" = $ver.Created
  80. "Created By" = $ver.CreatedBy
  81. "Delete Flag" = "Do not Delete"
  82. "Date Flag" = "Does not Match"
  83. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  84. }
  85. $results += New-Object PSObject -Property $RowDetails
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. $TotalSizeSaved = @{
  93. "Document Name" = ""
  94. "Version Number" = ""
  95. "Created Date" = ""
  96. "Created By" = ""
  97. "Delete Flag" = ""
  98. "Date Flag" = ""
  99. "Version Size (Kb)" = "Total Size = " + $storageSaved
  100. }
  101. $results += New-Object PSObject -Property $TotalSizeSaved
  102. $results | export-csv -Path $myPath -NoTypeInformation
  103. Write-Host "---------------------------------------------------------------"
  104. Write-Host $storageSaved " MB can be saved from this library"
  105. Write-Host "---------------------------------------------------------------"

Code for Delete item versions in SharePoint using PowerShell – Recycle Mode

  1. #################################################################
  2. #### Delete-Item-Versions-in-SharePoint using PowerShell - Recycle Mode ####
  3. #################################################################
  4. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
  5. {
  6. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  7. }
  8. $myWeb = "http://mysharepoint.com/sites/myweb1"
  9. $myList = "Fantastic Library"
  10. $myDate = "2014-01-01"
  11. $myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"
  12. $results = @()
  13. $storageSaved = 0
  14. $SPWeb = Get-SPWeb $myWeb
  15. foreach ($list in $SPWeb.Lists)
  16. {
  17. if ($list.Title -eq $myList)
  18. {
  19. Write-Host "Name: " $list.Title
  20. Write-Host "Type: " $list.BaseType
  21. $itemColl = $list.Items
  22. foreach ($item in $itemColl)
  23. {
  24. foreach ($ver in $item.Versions)
  25. {
  26. #this block for non-current versions
  27. if (!$ver.IsCurrentVersion)
  28. {
  29. if ((Get-Date $ver.Created) -lt (Get-Date $myDate))
  30. {
  31. $RowDetails = @{
  32. "Document Name" = $item.Name
  33. "Version Number" = $ver.VersionLabel
  34. "Created Date" = $ver.Created
  35. "Created By" = $ver.CreatedBy
  36. "Status" = "Moved to Recycle Bin"
  37. "Date Flag" = "Match"
  38. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  39. }
  40. $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)
  41. $storageSaved = $storageSaved + $tempSize
  42. $results += New-Object PSObject -Property $RowDetails
  43. $ver.Recycle()
  44. }
  45. else
  46. {
  47. $RowDetails = @{
  48. "Document Name" = $item.Name
  49. "Version Number" = $ver.VersionLabel
  50. "Created Date" = $ver.Created
  51. "Created By" = $ver.CreatedBy
  52. "Delete Flag" = "Ignored as Date does not match"
  53. "Date Flag" = "Does not Match"
  54. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  55. }
  56. $results += New-Object PSObject -Property $RowDetails
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. $TotalSizeSaved = @{
  64. "Document Name" = ""
  65. "Version Number" = ""
  66. "Created Date" = ""
  67. "Created By" = ""
  68. "Delete Flag" = ""
  69. "Date Flag" = ""
  70. "Version Size (Kb)" = "Total Size = " + $storageSaved
  71. }
  72. $results += New-Object PSObject -Property $TotalSizeSaved
  73. $results | export-csv -Path $myPath -NoTypeInformation
  74. Write-Host "---------------------------------------------------------------"
  75. Write-Host $storageSaved " MB can be saved from this library"
  76. Write-Host "---------------------------------------------------------------"

Delete item versions in SharePoint using PowerShell – Permanent Delete Mode

  1. #################################################################
  2. #### Delete-Item-Versions-in-SharePoint using PowerShell - Recycle Mode ####
  3. #################################################################
  4. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
  5. {
  6. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  7. }
  8. $myWeb = "http://mysharepoint.com/sites/myweb1"
  9. $myList = "Fantastic Library"
  10. $myDate = "2014-01-01"
  11. $myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"
  12. $results = @()
  13. $storageSaved = 0
  14. $SPWeb = Get-SPWeb $myWeb
  15. foreach ($list in $SPWeb.Lists)
  16. {
  17. if ($list.Title -eq $myList)
  18. {
  19. Write-Host "Name: " $list.Title
  20. Write-Host "Type: " $list.BaseType
  21. $itemColl = $list.Items
  22. foreach ($item in $itemColl)
  23. {
  24. foreach ($ver in $item.Versions)
  25. {
  26. #this block for non-current versions
  27. if (!$ver.IsCurrentVersion)
  28. {
  29. if ((Get-Date $ver.Created) -lt (Get-Date $myDate))
  30. {
  31. $RowDetails = @{
  32. "Document Name" = $item.Name
  33. "Version Number" = $ver.VersionLabel
  34. "Created Date" = $ver.Created
  35. "Created By" = $ver.CreatedBy
  36. "Status" = "Deleted Successfully"
  37. "Date Flag" = "Match"
  38. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  39. }
  40. $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)
  41. $storageSaved = $storageSaved + $tempSize
  42. $results += New-Object PSObject -Property $RowDetails
  43. $ver.Delete()
  44. }
  45. else
  46. {
  47. $RowDetails = @{
  48. "Document Name" = $item.Name
  49. "Version Number" = $ver.VersionLabel
  50. "Created Date" = $ver.Created
  51. "Created By" = $ver.CreatedBy
  52. "Delete Flag" = "Ignored as Date does not match"
  53. "Date Flag" = "Does not Match"
  54. "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)
  55. }
  56. $results += New-Object PSObject -Property $RowDetails
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. $TotalSizeSaved = @{
  64. "Document Name" = ""
  65. "Version Number" = ""
  66. "Created Date" = ""
  67. "Created By" = ""
  68. "Delete Flag" = ""
  69. "Date Flag" = ""
  70. "Version Size (Kb)" = "Total Size = " + $storageSaved
  71. }
  72. $results += New-Object PSObject -Property $TotalSizeSaved
  73. $results | export-csv -Path $myPath -NoTypeInformation
  74. Write-Host "---------------------------------------------------------------"
  75. Write-Host $storageSaved " MB can be saved from this library"
  76. Write-Host "---------------------------------------------------------------"

Output

It is recommended that these codes are run using the system account, or by any user who has sufficient privileges in the site. Once completed, the report log will be generated in the path specified.

SharePoint

The PowerShell codes for this can also be found in my GitHub repo.