16

As we all know, on a *nix system, rm -rf some_directory removes some_directory and all files beneath it recursively, without asking for confirmation.

What is the equivalent of this command in Powershell?

Note that the answers given here for cmd (using rmdir, etc.) do not work in Powershell. Though Powershell does alias rmdir to Remove-Item (presumably with some switch; not sure which), it doesn't alias cmd-style switches like /s.

senshin
  • 756
  • 2
  • 8
  • 22

4 Answers4

16

This is probably what you're looking for. Seems like a little effort with a search engine would've reached the same conclusion.

Remove-Item C:\MyFolder -Recurse -Force

Or, as a shorthand:

rm <directory-path> -r -Force

(At least in some versions of Powershell you cannot abbreviate "-Force" to "-f" because it is ambiguous with "-Filter".) For more information see the Remove-Item help page.

Mark Foskey
  • 123
  • 4
Tim Ferrill
  • 868
  • 8
  • 11
  • 2
    Well, yes, I did try searching, but two-character strings like "rm" and "rf" don't exactly produce the best search results. Anyway, a bit more searching turned up [this SO answer](http://stackoverflow.com/a/1752751/1535629) - do you happen to know whether the `-Recurse` parameter is necessary, and if so, why? – senshin Jun 15 '14 at 00:05
  • 2
    Actually both are probably best. The `-Force` switch actually deals with hidden files and the like, while `-Recurse` deals with subfolders. – Tim Ferrill Jun 15 '14 at 00:08
  • 3
    The -f is option is ambiguous (maybe on newer OS), b/w -Filter and -Force. – Devesh Khandelwal Oct 25 '15 at 18:45
  • The only pattern that worked for me was `rm -r -Force` – Kip Sep 21 '17 at 18:31
5

The closest command in Powershell is:

try {
    Remove-Item -Recurse -ErrorAction:Stop C:\some_directory
} catch [System.Management.Automation.ItemNotFoundException] {}

rm -rf in Unix means remove a file and also:

-r, -R, --recursive   remove directories and their contents recursively
-f, --force           ignore nonexistent files and arguments, never prompt

Remove-Item -Force is not the same as rm -f.

-Force Forces the cmdlet to remove items that cannot otherwise be changed, such as hidden or read-only files or read-only aliases or variables.

To demonstrate that -Force does not "ignore nonexistent files and arguments, never prompt", if I do rm -r -Force thisDirectoryDoesntExist, it results in this error:

rm : Cannot find path 'C:\thisDirectoryDoesntExist' because it does not exist.

A one-liner is rm -r -ErrorAction:SilentlyContinue, but this will throw away errors that are not does-not-exist errors.

MakotoE
  • 243
  • 2
  • 9
  • `-ea 0` is equivalent to `-ErrorAction SilentlyContinue` if you're optimizing for fewest (non tab-completed) keystrokes as with `rm -r` – Peter Vandivier Feb 24 '23 at 18:13
1

You should use an explicit -force key instead of -f because otherwise, Powershell would be at a loss whether it was a -Filter or a -Force.

rm <path> -r -Force

wh1t3cat1k
  • 181
  • 1
  • 2
  • 9
1

This is the one-liner that behaves like rm -rf. It first checks for the existence of the path and then tries removing it.

if (Test-Path ./your_path) { rm -r -force ./your_path}
Amin Ya
  • 153
  • 5