Link to home
Start Free TrialLog in
Avatar of MisUszatek
MisUszatekFlag for United States of America

asked on

Help needed rewriting PowerShell script for Sharepoint

Can somebody please help rewrite the script from the following link:

http://www.mukalian.com/blog/post/2011/04/10/PowerShell-Script-to-Delete-All-Versions-in-a-Document-Library.aspx

It targets all libraries in a site collection. I need to be able to target a specific library in a specific site.
Avatar of Carlo-Giuliani
Carlo-Giuliani
Flag of Canada image

The following should do it.

Execute the first line by itself to be sure you have the right doc library.

$list = get-splist -URL http://site/sites/subsite -Title "Document Library Title"
    # loop through each item
    foreach ($item in $list.Items)
    {
      # work with the file object as we're in a document library
      $file = $item.File
      # delete all versions
      $file.Versions.DeleteAll()
    }
$list.Dispose();

Open in new window

Avatar of MisUszatek

ASKER

This is the error I get when running it:

The term 'get-splist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Should this be Get-SPWeb?
You need to either use the "SharePoint 2010 Management Shell" to run the script, or start your script with the command
add-pssnapin microsoft.sharepoint.powershell

Open in new window


Either way, it has to be done on a server with SharePoint 2010 code installed (though not necessarily in a farm).
ASKER CERTIFIED SOLUTION
Avatar of Carlo-Giuliani
Carlo-Giuliani
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It seems to be working but I got this error at the end:

Method invocation failed because [Microsoft.SharePoint.SPDocumentLibrary] doesn't contain a method named 'Dispose'.
At line:14 char:14
+ $list.Dispose <<<< ();
    + CategoryInfo          : InvalidOperation: (Dispose:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Can this line be safely removed?
Yes, some objects don't dispose.