0

I am trying to empty the contents of a list of folders that I have in a text file leaving the empty folder itself alone, using these following commands

$Targetfolder = "\\a\b\test
$includefiles = "\\c\list.txt"
Remove-Item $TargetFolder\* -Force -Recurse -Include (Get-Content $includefiles) -Verbose

It is removing an empty directory but not directories which have contents I also tried

Get-ChildItem $TargetFolder -Include (Get-Content $includefiles) -Recurse | ForEach  {$_.Delete()}

Shows directory not empty error with this

Please help!!

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    Does this answer your question? [Cannot remove item. The directory is not empty](https://stackoverflow.com/a/38141548/934946) – Sage Pourpre Jun 21 '22 at 19:02
  • 1
    @SagePourpre, I've switched the duplicate you've proposed to a different one, because I believe the answer to the former to neither explain the underlying problem nor to offer effective workarounds (at least historically - since Windows 10 20H2, no workarounds are needed anymore).If you feel there's still a good reason to link to the former, let me know, and we can add it as a another duplicate. – mklement0 Jun 22 '22 at 02:30

0 Answers0