Deleting specific files from Sharepoint on-premises library

Starting from this request, bellow you will find the script needed to try 2 option

The first one , the Powershell script will read the csv file and for each entry, fill parse the SharePoint library for the file

The second one, will use CAML queries to locate the specific file

For both of them, the measure command will show the time needed to resolve the requests.

Even for small libraries you will see that the second option is faster.
For large libraries and for long csv file, it is recommended yo use queries.

<#
DESCRIPTION
This script can be useful to search specific files in sharepoint on-premises, in a specific site.
The files are located in the csv file. The mandatoryy parameters are:
– $SiteUrl     : the URL for the site
– $LibraryName : the library where the files are located
– $Filename    : the csv which contains the files
The files in the csv files need to be with the full url
If the site is the root site , you can use $myweb and $mylist variables
This script can be use as it is or can be changed based on your needs. For more details please feel free to contact me
Romeo Donca, May 2017
romeodonca@outlook.com
http://www.romeodonca.ro
#>
 param(
[Parameter(Mandatory=$true,HelpMessage=”The site URL”)][string]$SiteUrl,
[Parameter(Mandatory=$true,HelpMessage=”The name of the library”)][string]$LibraryName,
[Parameter(Mandatory=$true,HelpMessage=”The csv file name”)][string]$Filename
)
Add-PSSnapin microsoft.sharepoint.powershell
###################################### parse all items
function deleting($mylist,$yoursourcefile)
{
    $files = @()
    foreach ($searchedfile in (Import-Csv $yoursourcefile))
    {
        #$myweb = get-spweb (($searchedfile.url).split(‘/’)[0]+”//”+$searchedfile.url.split(‘/’)[2])
        #$mylist = $myweb.Lists | Where-Object {$_.Title -match ($searchedfile.url).split(‘/’)[3]}
        foreach ($file in $mylist.items)
        {
            if (($searchedfile.url) -eq (($searchedfile.url).split(‘/’)[0]+”//”+($searchedfile.url).split(‘/’)[2]+”/”+$file.url))
            {
                “Deleting: ” +  $searchedfile.url
                $files = $files + $file
                break
            }
        }
    }
    foreach ($item in $files)
    {
        $item.Delete()
    }
}
#############################filter items using CAML queries
function deleting_with_queries($mylist,$yoursourcefile)
{
    $files = @()
    foreach ($searchedfile in (Import-Csv $yoursourcefile))
    {
        #$myweb = get-spweb (($searchedfile.url).split(‘/’)[0]+”//”+$searchedfile.url.split(‘/’)[2])
        #$mylist = $myweb.Lists | Where-Object {$_.Title -match ($searchedfile.url).split(‘/’)[3]}
        $query = New-Object Microsoft.SharePoint.SPQuery
        $query.ViewAttributes=”Scope=’RecursiveAll'”
        $caml ='<Where><Eq><FieldRef Name=”EncodedAbsUrl”/><Value Type=”Text”>’+$searchedfile.url+'</Value></Eq></Where>’
        $query.Query = $caml
        $query.RowLimit = 1
        $file = $mylist.GetItems($query)
        $files = $files + $file
    }
    foreach ($item in $files)
    {
        $item.Delete()
    }
}
function Show-Menu
{
     cls
     Write-Host “================================”
     Write-Host “1: Press ‘1’ for Deleting using Powershell parsing the list “
     Write-Host “2: Press ‘2’ for Deleting using Powershell and queries”
     Write-Host “Q: Press ‘Q’ to quit.”
}
do
{
    $myweb = Get-SPWeb $SiteUrl
    $mylist = $myweb.Lists[$LibraryName]
     Show-Menu
     $input = Read-Host “Please make a selection”
     switch ($input)
     {
           ‘1’ {
                Measure-Command{
                    deleting $mylist $filename
                }
                ‘You chose option: Powershell parsing the list’
           } ‘2’ {
                Measure-Command{
                    deleting_with_queries $mylist $filename
                }
                ‘You chose option: Powershell and queries’
           } ‘q’ {
                return
           }
     }
     pause
}
until ($input -eq ‘q’)
This entry was posted in Powershell, Query, Sharepoint. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *