Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

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 -f

For more information see the Remove-Item help page.

4

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.

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

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}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy