Windows OS

“rm -rf” Command Equivalent for Windows

The “rm -rf” is a Linux-based command primarily used to remove directories and files. “-rf” are the options supported by the “rm” command, where “r” is added to remove folders and files recursively similar to Windows, while “f” is used to delete directories as well as file forcefully. In the Windows Command Prompt, we can replace the “rm” command with the “rd” command and the “f” option with “/s” to delete files and folders forcefully.

In this article, we will illustrate the Windows commands that are equivalent to “rm -rf”:

So, let’s get started!

“rm -rf” Command Equivalent for Windows in Command Prompt

The “rm -rf” command is Linux based command to remove directories along with stored files. Let’s have a look at the command used to remove files and directories in Windows using Command Prompt:

The following section will discuss the usage of the above-given commands in Command Prompt.

How to Use rd Command in Windows?

The “rd” command can be used to remove the directory and the related stored files. Here, the “/s” option will remove all files along with the directory, and “/q” will avoid getting confirmation:

>rd /s /q "C:\Users\anuma\OneDrive\Desktop\C program"

How to Use rmdir Command in Windows?

The “rmdir” command can be utilized in Command Prompt to remove directories and files. Here are some examples of using the rmdir command.

Example 1: Use rmdir Command with Path

The “rmdir” command is equivalent to the “rm -rf” command as it can also remove directories along with stored files with the help of the specified path:

>rmdir /s /q "C:\Users\anuma\OneDrive\Desktop\C program"

Example 2: Use rmdir Command With Directory or File Name

You can utilize the “rmdir” command to remove the directory by specifying its name instead of a path. If the mentioned directory exists, it will be deleted using the “if exist” command:

>if exist C program (rmdir /s/q Cprogram)

Example 3: Use rmdir Command With a Specific Location

Move to the directory where the desired file or directory exists using the “cd” command:

>cd C:\Users\anuma\OneDrive\Desktop

Then, execute the “rmdir” command to remove the directory by adding the directory name:

>rmdir /S /Q "C program1"

Now, let’s head towards the second method.

“rm -rf” Command Equivalent for Windows in PowerShell

In Windows PowerShell, the “rm -rf” can be equivalent to the following list of commands:

  • Remove-Item” command
  • rm” command
  • Delete() method

How to Use Remove-Item Command in Windows?

To utilize the “Remove-Item” command in Windows, follow the below-mentioned examples.

Example 1: Use Remove-Item Command with Path

The “Remove-Item” command is equivalent to the “rm -rf” command as it can be used to remove folders along with stored files. For this purpose, provide the path where the file or folder exists.

To remove a folder forcefully, add the “-Force” option as shown below:

> Remove-Item -Path E:\SampleFolder -Force

Example 2: Use Remove-Item Command With a Specific Location

To remove a file or folder by specifying its name, first, open the directory where the folder exists using the “cd” command:

> cd E:

Run the “Remove-Item” command and specify the folder name. Here, the “-Recurse” option can be used to delete files and folders recursively. If the folder has any hidden files, then utilize the “-Force” option to delete the folder and files forcefully:

> Remove-Item -Recurse -Force "Cprogram"

Example 3: Use Remove-Item and Get-ChildItem Command

Use the “Get-ChildItem” command to retrieve the file or folder from the specified path. Then, to delete the file or folder, use the pipe operator “|” to transfer the file’s information to the “Remove-Item” command:

> Get-ChildItem E:\Cprogram -Recurse -Filter "mydata" | Remove-Item -Force -Recurse

How to Use Delete() Method in Windows?

Use the “Get-ChildItem” command to access the desired folder, then call the “Delete()” method to remove its files and folders:

> (Get-ChildItem E:\Cprogram).Delete()

How to Use rm Command in Windows?

The “rm” command is also equivalent to the “rm -rf” command and can be used to remove directories and files. In this command, the “-r” option is used as “Recursive” and “-fo” is utilized to “-Force” the file removal:

> rm -r -fo E:\Cprogram

We have demonstrated the commands that are equivalent to the “rm -rf”.

Conclusion

Many Windows commands that can be executed from the Command Prompt or Windows PowerShell are equivalent to “rm -rf.” In the Command Prompt, you can use “rd” or “rmdir” commands to remove directory as well as files. In Windows PowerShell, you can replace the “rm -rf” command either with the “rm -r -fo directory_path” or the “Remove-Item” command. We have elaborated on the “rm -rf” Command Equivalent for Windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.