Delete all files and folders but exclude a subfolder

98,623

Solution 1

Get-ChildItem -Path  'C:\temp' -Recurse -exclude somefile.txt |
Select -ExpandProperty FullName |
Where {$_ -notlike 'C:\temp\foldertokeep*'} |
sort length -Descending |
Remove-Item -force 

The -recurse switch does not work properly on Remove-Item (it will try to delete folders before all the child items in the folder have been deleted). Sorting the fullnames in descending order by length insures than no folder is deleted before all the child items in the folder have been deleted.

Solution 2

In PowerShell 3.0 and below, you can try simply doing this:

Remove-Item -recurse c:\temp\* -exclude somefile.txt,foldertokeep

Unless there's some parameter I'm missing, this seems to be doing the trick...

Edit: see comments below, the behavior of Remove-Item has changed after PS3, this solution doesn't seem applicable anymore.

Solution 3

Select everything excluding what needs to be keep and pipe that to a delete command.

Say you have those folders

C:.
├───delme1
│   │   delme.txt
│   │
│   └───delmetoo
├───delme2
├───keepme1
│       keepmetoo.txt
│
└───keepme2

To delete everything but preserve the keepme1 and keepme2 folders.

Get-ChildItem -Exclude keepme1,keepme2 | Remove-Item -Recurse -Force

Other solutions are fine but I found this easy to understand and to remember.

Solution 4

I used the below and just removed -Recurse from the 1st line and it leaves all file and sub folders under the exclude folder list.

   Get-ChildItem -Path "PATH_GOES_HERE" -Exclude "Folder1", "Folder2", "READ ME.txt" | foreach ($_) {
       "CLEANING :" + $_.fullname
       Remove-Item $_.fullname -Force -Recurse
       "CLEANED... :" + $_.fullname
   }

Solution 5

Yes I know this is an old thread. I couldn't get any of the answers above to work in Powershell 5, so here is what I figured out:

Get-ChildItem -Path $dir -Exclude 'name_to_ignore' |
ForEach-Object {Remove-Item $_ -Recurse }

This moves the -Recurse to Remove-Item instead of where the items are found.

Share:
98,623
Mathias F
Author by

Mathias F

Guy from Germany who loves brooklynisms. Do they realy use the term Coney Island Whitefish?

Updated on October 07, 2021

Comments

  • Mathias F
    Mathias F over 2 years

    I have a folder where I need to delete all files and folders except a small list of files and folders.

    I can already exclude a list of files, but don't see a way to exclude a folder and its contents.

    Here is the folder structure:

    |-C:\temp
     \-C:\temp\somefile.txt
     \-C:\temp\someotherfile.txt
    | |-C:\temp\foldertodelete
       \-C:\temp\foldertodelete\file1.txt
    | |-C:\temp\foldertokeep
    |  \-C:\temp\foldertokeep\file2.txt
    

    I want to keep somefile.txt and the folder foldertokeep and its content.

    This is what I have right now:

    Get-ChildItem -Path  'C:\temp' -Recurse -exclude somefile.txt | Remove-Item -force -recurse
    

    This really does not delete somefile.txt. Is there a way to exclude folder foldertokeep and its content from the delete list?