Sunday, February 22, 2015

PowerShell script for deleting documents in a SharePoint Document Library based on dates


Below script works for deletion but it is unable to delete folders.

----------------------------------------------------------
$site = Get-SPSite "http://spdev1/sites/test"
foreach ($web in $site.AllWebs) {

# update the value for targeted Doc Lib $_.Title -eg "Library Title"
   $Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary" -And $_.Title -eq "Demant"}
    foreach ($library in $Libraries) {
       Write-Output "Getting files from $($library.Title)"

# get all the word & Excel files older than October 28, 2014 update below line
       $Files = $library.Items | where {($_.FileSystemObjectType -eq "File") -And ($_.File -Like "*.docx" -Or $_.File -Like "*.doc" -Or $_.File -Like "*.xls" -Or $_.File -Like "*.xlsx") -And ($_.File.TimeCreated -lt "10-28-2014")}
        foreach ($file in $Files) {
            Write-Output "Deleting file $($file.Name)..."
            $file.Delete()

        } # foreach file

    } # foreach library

 } # foreach web



No comments:

Post a Comment