Powershell Command: rm -rf

PowershellCommandCmdRm

Powershell Problem Overview


rm is to remove item, but what is the parameter -rf do or signify?


Whenever I typed help -rf it printed the entire list of available commands in powershell. What happens if you type rm -rf in powershell? From reading around I've gathered that it will delete everything on the drive? I'm not sure?

Also, is rm -rf same as rm -rf /

Powershell Solutions


Solution 1 - Powershell

PowerShell isn't UNIX. rm -rf is UNIX shell code, not PowerShell scripting.

  • This is the documentation for rm (short for Remove-Item) on PowerShell.
  • This is the documentation for rm on UNIX.

See the difference?

On UNIX, rm -rf alone is invalid. You told it what to do via rm for remove with the attributes r for recursive and f for force, but you didn't tell it what that action should be done on. rm -rf /path/to/delete/ means rm (remove) with attributes r (recursive) and f (force) on the directory /path/to/remove/ and its sub-directories.

The correct, equivalent command on PowerShell would be:

rm C:\path\to\delete -r -fo

Note that -f in PowerShell is ambiguous for -Filter and -Force and thus -fo needs to be used.

Solution 2 - Powershell

You have to use:

Remove-Item C:\tmp -Recurse -Force

or (short)

rm C:\tmp -Recurse -Force

Solution 3 - Powershell

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}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBK_View Question on Stackoverflow
Solution 1 - PowershellMahmoud Al-QudsiView Answer on Stackoverflow
Solution 2 - PowershellKai SternadView Answer on Stackoverflow
Solution 3 - PowershellAminView Answer on Stackoverflow