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
I use this approach assuming you want to exclude some files or folders at root level but then you want to delete everything inside them.
C:.
├───delme1
│ │ delme.txt
│ │
│ └───delmetoo
├───delme2
├───folder1
│ keepmesafe.txt
│
└───folder2
So you want to delete everything and preserver the folder1 and folder2
Get-ChildItem -Exclude folder1,folder2 | Remove-Item -Recurse -Force
There are many other solutions but I found this easy to understand and remember.