Link to home
Start Free TrialLog in
Avatar of stratoman
stratomanFlag for United States of America

asked on

Delete files in a sharepoint library except one folder

I am trying to use a script. but i only want to delete files in the root of document library and not one specific folder.  basically when the script runs, it looks at the library and delete all files except in a folder called Archive. I use below script but it also delete files in the Archive folder. Can someone help me?

$web = Get-SPWeb "my site"
$Libraries = $web.Lists | where {$_.Title -eq "Shared Documents" }
 
foreach ($library in $Libraries) {
 
    Write-Output "Getting files from $($library.Title)"
        $Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.pdf" -Or $_.File -Like "*.docx" -Or $_.File -Like "*.doc")}
     foreach ($file in $Files) {
        Write-Output "Deleting file $($file.Name)..."
        $file.Delete()
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stratoman

ASKER

Rainer,

i have no words to describe how happy i am now. The script worked perfectly. and Thanks to you. I was looking for this File.ParentFolder.Name property and i just couldn't. how can i get all the properties of a library in sharepoint? Great help to me and thank you again!!!!!!!!!!!
Hi,
no problem.
It is not so easy to get all properties as each and every different property type can have additional/other properties.
But the MSDN object reference is really good and helpful - sometimes hard to navigate (I normally open new tabs and end up with at least 6-10 :-) )
http://msdn.microsoft.com/en-us/library/Microsoft.SharePoint.aspx

Thanks and HTH
Rainer

PS: Please do not forget to mark the question as answered.
Thanks again for the prompt reply!