SharePoint Delete files with and without conditions using PowerShell

The below PowerShell script you can use to delete the files in the document library

$web = Get-SPWeb "SiteURL"
$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}

foreach ($library in $Libraries) {

    Write-Output "Getting files from $($library.Title)"
    $Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
    foreach ($file in $Files) {
        Write-Output "Deleting file $($file.Name)..."
        $file.Delete()
    }
}

Conditions 1

$Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.docx" -Or $_.File -Like "*.docm" -Or $_.File -Like "*.doc")}

Condition 2

$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary" -And $_.Title -eq "Resumes"}


Comments

Popular posts from this blog

This content database has a schema version which is not supported in this farm

Export all documents/Folders permissions to CSV using SharePoint PowerShell

Install WSP in SharePoint using Powershell