Friday, December 9, 2011

SharePoint PowerShell: Delete old versions of documents and items

Project Requirements:

Most of the doucments were having lots of versions so the need was to remove all the existing versions except the latest version.

You can change this script to complete all the operation at one time means for all the libraries and lists under a web or all the site collections or all the web applications with in a farm.

The following script will remove all the existing versions of the documents except latest one.
PowerShell Command Prompt:

get-spWeb http://xyz-vm:1000/
ForEach-Object {ForEach($_.list in $_.Lists){If($_.EnableVersioning -eq $True){if($_.Title -eq "Shared Documents"){Write-host "List:"$_.Title "- List ID"$_.Id;$_.MajorVersionLimit = 1;$_.Update();ForEach($_.item in $_.items){$_.item.URL;$_.update()}}}}}

http://xyz-vm:1000 - Replace with your web name
Shared Documents - Replace with your list or library name
MajorVersionLimit = 1 - Change 1 to your specific limit


No comments: