What is the equivalent of rm -rf in Powershell?
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
.
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.
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