DEV Community

Tomoyuki Aota
Tomoyuki Aota

Posted on • Edited on

Creating a short PowerShell command equivalent to "rm -rf" in Bash

(A Japanese translation is available here.)

In Bash, rm -rf deletes a file or a directory. In PowerShell, I would like to do the same thing in a short command as well. Therefore, I created a PowerShell function to do the job.

Introduction

Let's say that I want to delete target. In PowerShell, the following command does the job.

Remove-Item -Recurse -Force target

Enter fullscreen mode Exit fullscreen mode

This is a long command even with tab completion. The following command is shortened version using officially provided aliases.

rm -r -fo target

Enter fullscreen mode Exit fullscreen mode

This is still long when compared to rm -rf in Bash.
Therefore, I decided to create a PowerShell function, rmrf, for this job.

How to create the command

PowerShell profile needs to be created.
echo $profile displays the file path of the profile. If the file does not exist, create it first.

In PowerShell profile, add the following function.

After editing the profile, restart PowerShell to reload the profile.

Result

Issuing the following command will delete target. It's short!

rmrf target

Enter fullscreen mode Exit fullscreen mode

Top comments (0)