none
Deleting files by path

    Question

  • Hi !

    I'm wondering if there's a way to use Powershell in SharePoint delete files based on a particular path ?

    I have a list of many specific files that need to be deleted, but can't see a way of deleting them by path.

    eg: 

    http://sharepoint/lib/folder1/123.docx

    http://sharepoint/lib/folder2/234.xls

    http://sharepoint/lib/folder3/567.pdf

    I've seen various ways of using the item.delete method for large sets of files based on a criteria such as a date, but can't see a way of adapting this for a long list of file names. 

    Thanks !

    Wednesday, May 03, 2017 3:17 PM

Answers

  • Hi

    these file names should be in a source file ( like a csv file or txt file )

    Please try this code, and let me know if you need more details

    foreach ($searchedfile in (Import-Csv yourfile.csv))
    {
    $myweb = get-spweb ($searchedfile.split('/')[0]+"//"+$searchedfile.split('/')[2])
    $mylist = $myweb.Lists | Where-Object {$_.Title -match $searchedfile.split('/')[3]}
    foreach ($file in $mylist.items)
    {
        $file.url
        if ($searchedfile -eq ($variable.split('/')[0]+"//"+$searchedfile.split('/')[2]+"/"+$file.url))
        {
        "deletig        " +  $file.url
        $file.Delete()
        }
    }
    }


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, May 03, 2017 9:34 PM

All replies

  • Hi

    these file names should be in a source file ( like a csv file or txt file )

    Please try this code, and let me know if you need more details

    foreach ($searchedfile in (Import-Csv yourfile.csv))
    {
    $myweb = get-spweb ($searchedfile.split('/')[0]+"//"+$searchedfile.split('/')[2])
    $mylist = $myweb.Lists | Where-Object {$_.Title -match $searchedfile.split('/')[3]}
    foreach ($file in $mylist.items)
    {
        $file.url
        if ($searchedfile -eq ($variable.split('/')[0]+"//"+$searchedfile.split('/')[2]+"/"+$file.url))
        {
        "deletig        " +  $file.url
        $file.Delete()
        }
    }
    }


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, May 03, 2017 9:34 PM
  • Hi

    I posted a real example for you, with 2 options

    first one , the script will parse the library

    and the second one use CAML queries which are faster

    Because in your csv file , the path could be not the shortest as in your example, the formula used first

    $mylist = $myweb.Lists | Where-Object {$_.Title -match $searchedfile.split('/')[3]}

    , will be no longer working

    Now the user will need to provide the site's URL and the library's name

    The script can be found here

    $mylist = $myweb.Lists | Where-Object {$_.Title -match $searchedfile.split('/')[3]}

    http://romeodonca.com/deleting-specific-files-from-sharepoint-on-premises-library/


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Tuesday, May 09, 2017 5:15 AM