Skip to content

Instantly share code, notes, and snippets.

@AdamNaj
Last active March 3, 2021 22:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamNaj/1bd3980a9a5dc5014c66 to your computer and use it in GitHub Desktop.
Save AdamNaj/1bd3980a9a5dc5014c66 to your computer and use it in GitHub Desktop.
Item Version Pruner
function Remove-OldItemVersion {
[CmdletBinding()]
param(
[Parameter(Position = 0,Mandatory = $true,ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[Sitecore.Data.Items.Item]$Item,
[Parameter(Position = 1,Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[int]$MaxVersions
)
process {
while ($Item.Versions.Count -gt $MaxVersions) {
$Item.Versions[$Item.Versions.GetVersionNumbers()[0]].Versions.RemoveVersion();
}
}
}
# items underneath this path to be pruned
$path = "master:\content"
#languages can be defined specifically like "en", "pl-PL' or with wildcards like "*"
$languages = "*"
#Check how many versions are there in each language in each of the items before the trim
Get-ChildItem $path -Language $languages -Recurse | ft name,language,@{ Name = "VerCount"; Expression = { $_.Versions.Count } }
#ACTION HAPPENS HERE
Get-ChildItem $path -Language $languages -Recurse | Remove-OldItemVersions -max 10
#Check how many versions are there in each language in each of the items after the trim
Get-ChildItem $path -Language $languages -Recurse | ft name,language,@{ Name = "VerCount"; Expression = { $_.Versions.GetVersionNumbers().Length } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment