I need a way to do the equivalent of a Unix/Linx rm -rf http://linux.die.net/man/1/rm
command on a given directory. I want to blow away the directory, all of its subdirectories and all files found therein. There may be read-only, hidden and/or system files that also need to be erased.
What is the correct PowerShell method to do that?
This works for me:
Remove-Item $folder -Recurse -Force -Confirm:$false
The Help on Remove-Item says:
The Recurse parameter of this cmdlet does not work properly.
Here’s my workaround.
Get-ChildItem $folder -Recurse -Force |
select -ExpandProperty FullName |
sort length -Descending |
foreach {Remove-Item -LiteralPath $_ -Force}
@rob: Almost! Â Your solution removed the files in the target directory but not the directory itself.
FWIW I have been using:
&cmd /c rmdir /s /q $folder
Which seems to work as well. Â I suppose it may be faster than the pure PS approach, though I haven’t timed it.