Created
April 7, 2021 07:21
-
-
Save webbson/532c66d562a6905387ca3a141bdd10ad to your computer and use it in GitHub Desktop.
This Sitecore PowerShell script removes old versions of items in all languages so that the items only contains a selected number of versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
This script will remove old versions of items in all languages so that the items only contains a selected number of versions. | |
#> | |
$item = Get-Item -Path "master:\content" | |
$dialogProps = @{ | |
Parameters = @( | |
@{ Name = "item"; Title="Branch to analyse"; Root="/sitecore/content/Home"}, | |
@{ Name = "count"; Value=10; Title="Max number of versions"; Editor="number"}, | |
@{ Name = "remove"; Value=$False; Title="Do you wish to remove items?"; Editor="check"} | |
) | |
Title = "Limit item version count" | |
Description = "Sitecore recommends keeping 10 or fewer versions on any item, but policy may dictate this to be a higher number." | |
Width = 500 | |
Height = 280 | |
OkButtonName = "Proceed" | |
CancelButtonName = "Abort" | |
} | |
$result = Read-Variable @dialogProps | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
$items = @() | |
Get-Item -Path master: -ID $item.ID -Language * | ForEach-Object { $items += @($_) + @(($_.Axes.GetDescendants())) | Where-Object { $_.Versions.Count -gt $count } | Initialize-Item } | |
$ritems = @() | |
$items | ForEach-Object { | |
$webVersion = Get-Item -Path web: -ID $_.ID -Language $_.Language | |
if ($webVersion) { | |
$minVersion = $webVersion.Version.Number - $count | |
$ritems += Get-Item -Path master: -ID $_.ID -Language $_.Language -Version * | Where-Object { $_.Version.Number -le $minVersion } | |
} | |
} | |
if ($remove) { | |
$toRemove = $ritems.Count | |
$ritems | ForEach-Object { | |
$_ | Remove-ItemVersion | |
} | |
Show-Alert "Removed $toRemove versions" | |
} else { | |
$reportProps = @{ | |
Property = @( | |
"DisplayName", | |
@{Name="Version"; Expression={$_.Version}}, | |
@{Name="Path"; Expression={$_.ItemPath}}, | |
@{Name="Language"; Expression={$_.Language}} | |
) | |
Title = "Versions proposed to remove" | |
InfoTitle = "Sitecore recommendation: Limit the number of versions of any item to the fewest possible." | |
InfoDescription = "The report shows all items that have more than <b>$count versions</b>." | |
} | |
$ritems | Show-ListView @reportProps | |
} | |
Close-Window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment