PowerShell equivalent to rm -rf

 

 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.

 

@poshoholic:

-recurse problem:

 

https://connect.microsoft.com/PowerShell/feedback/details/87456/remove-item-recurse-and-copy-item-recurse-exclude-not-acting-as-expected

https://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/a777a47b-64f9-4756-849d-a4c7374e757c/

http://technet.microsoft.com/en-us/library/hh849765.aspx

Looks like it won’t be fixed, either