17

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.

4 Answers 4

19

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.

4
  • 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 - do you happen to know whether the -Recurse parameter is necessary, and if so, why?
    – senshin
    Jun 15, 2014 at 0:05
  • 2
    Actually both are probably best. The -Force switch actually deals with hidden files and the like, while -Recurse deals with subfolders. Jun 15, 2014 at 0:08
  • 3
    The -f is option is ambiguous (maybe on newer OS), b/w -Filter and -Force. Oct 25, 2015 at 18:45
  • The only pattern that worked for me was rm <dir> -r -Force
    – Kip
    Sep 21, 2017 at 18:31
6

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.

1
  • 1
    -ea 0 is equivalent to -ErrorAction SilentlyContinue if you're optimizing for fewest (non tab-completed) keystrokes as with rm -r Feb 24, 2023 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

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .