Delete All Versions of a SharePoint File Using PowerShell

There may be instances where you need to remove all previous versions of a file. Well, this is the PowerShell script to do it. This will only work for SharePoint on-premises however.

param(
[string] $UrlToFile
)

$Site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $UrlToFile
$Web = $Site.OpenWeb()
$SPFile = $Web.GetFile($UrlToFile)

Write-Host “Deleting all versions for file $($UrlToFile)…”

# Remove all versions for file…
$SPFile.Versions.DeleteAll()

# Dispose of the web object
$Web.Dispose()

# Dispose of the site object
$Site.Dispose()

Save this off as something rememberable, such as Remove-FileVersions.ps1, and pass it the full URL to the file you wish to remove all versions from, for example:

.\Remove-FileVersions.ps1 “http://mysharepoint.com/sites/foo/Documents/Document_1.docx”

Enjoy!

Advertisement

Office 365, SharePoint Online and SharePoint 2013 On-Premises Feature Matrix

Andrew Connell released an updated Excel Spreadsheet today, which shows the (filterable) differences between Office 365 plans, SharePoint Online, and SharePoint 2013 on-premises. This is a great resource to keep handy, and keep you honest 🙂

image 

Read more at http://www.andrewconnell.com/blog/office-365-sharepoint-online-and-sharepoint-2013-on-prem-feature-matrix-updated#RLqeQWPtJtHiWVwi.99

%d bloggers like this: