none
How to remove specific file types (i.e. docx) from multiple specific Sharepoint libaries using Powershell?

    Question

  • I am a newbie when it comes to Powershell so I'm asking for help. In Sharepoint 2010 I have several document libraries in a single site collection that all start with the name of 'PT-' and in those libraries are specific files that I need to delete like *.xyz. How can I loop through the libraries and remove the files based on the file extension using Powershell?

    Any help would be appreciated and let me know if I need to supply more information.

    Thanks, Mike

    Wednesday, June 12, 2013 9:31 PM

Answers

  • OK, so then go through every library that starts with "PT-". Are all these libraries in the same site?

    Something like this will delete all the xyz files in all the PT-* libraries in the root site. You could iterate through sites if there are other sites.

    $site = Get-SPSite "http://example"
    $Libraries = $site.RootWeb.Lists | where {$_.BaseType -eq "DocumentLibrary" -And $_.Title -Like "PT-*"}
    
    foreach ($library in $Libraries) {
    
    	Write-Output "Getting files from $($library.Title)"
    	$Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And $_.File -Like "*.xyz"}
    	foreach ($file in $Files) {
    		Write-Output "Deleting file $($file.Name)..."
    		$File.Delete()
    	}
    }
    



    Jason Warren
    Infrastructure Architect

    • Proposed as answer by Mohammad Nizamuddin Friday, June 14, 2013 2:40 AM
    • Marked as answer by MGoetz Wednesday, June 19, 2013 2:21 PM
    Friday, June 14, 2013 1:37 AM

All replies

  • Do you want to remove all the files from all document libraries? Are there more than one site?

    One way is to go through library in every site and then then check every item to see if it matches your filename pattern. If it does, delete the item.


    Jason Warren
    Infrastructure Architect

    Wednesday, June 12, 2013 11:13 PM
  • There can be multiple file types in the library but I want to remove specific files that have a particular extension like all jpg's however I don't want to traverse through all libraries but all the ones that start with a particular prefix such as all document libraries that have a prefix of 'PT-' and it would just be for a single site.

    I'm hoping somebody can point me to a site with a powershell script that does this or something close to it or can whip something up quick.

    Thanks,

    Mike

     

    Thursday, June 13, 2013 2:14 PM
  • OK, so then go through every library that starts with "PT-". Are all these libraries in the same site?

    Something like this will delete all the xyz files in all the PT-* libraries in the root site. You could iterate through sites if there are other sites.

    $site = Get-SPSite "http://example"
    $Libraries = $site.RootWeb.Lists | where {$_.BaseType -eq "DocumentLibrary" -And $_.Title -Like "PT-*"}
    
    foreach ($library in $Libraries) {
    
    	Write-Output "Getting files from $($library.Title)"
    	$Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And $_.File -Like "*.xyz"}
    	foreach ($file in $Files) {
    		Write-Output "Deleting file $($file.Name)..."
    		$File.Delete()
    	}
    }
    



    Jason Warren
    Infrastructure Architect

    • Proposed as answer by Mohammad Nizamuddin Friday, June 14, 2013 2:40 AM
    • Marked as answer by MGoetz Wednesday, June 19, 2013 2:21 PM
    Friday, June 14, 2013 1:37 AM
  • Jason,

    Awesome stuff! I've been testing your script and it works perfect. I'm amazed with what little code is needed to accomplish the task. The road I was going down was going to create a script monster.

    I appreciate your time with this.

    Thanks,

    Mike

    Wednesday, June 19, 2013 2:21 PM
  • You're welcome. :D

    Wednesday, June 19, 2013 5:32 PM
  • After spent several hours searching for solution and tried out many suggestions without success,  I found this and it worked perfectly.  What's so great about this is that you make it so powerful but simple to understand compare to many other suggestions i found.

    Thanks so much

    Swanl

     


    Swanl

    Tuesday, February 17, 2015 6:36 PM