DEV Community

Tomoyuki Aota
Tomoyuki Aota

Posted on • Edited on

7

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.

function rmrf {
<#
.DESCRIPTION
Deletes the specified file or directory.
.PARAMETER target
Target file or directory to be deleted.
.NOTES
This is an equivalent command of "rm -rf" in Unix-like systems.
#>
Param(
[Parameter(Mandatory=$true)]
[string]$Target
)
Remove-Item -Recurse -Force $Target
}

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay