I know that I can use rm -r -fo to remove a folder and all its content in PowerShell. But is there any way I can use rm -rf in PowerShell?
I tried to add the below scripts into my $Profile, but both did not work.
First try:
function rm -rf() { rm -r -fo }Second try:
Set-Alias rm -rf "rm -r -fo"
As suggested in the comments, you can write a proxy function, that adds the
-rfparameter toRemove-Item. If you then set the aliasrmto point to your proxy function instead ofRemove-Item, you will be able to userm -rflike in Unix, while still having the opportunity to use all other parameters ofRemove-Item. You will basically haveRemove-Itemwith the additional parameter-rf.As documented in iRon's excellent Q&A, you can generate the proxy function body with the following commands:
Which will give you:
You can paste this – as is – directly into your own function definition and you will already have a function that does the same as
Remove-Item.To add the parameter
-rf, add the the following parameter definition:To not pass it to
Remove-Itemdirectly, add the following line to thedynamicparamblock:Then you just need to handle the call of
Remove-Item, when the-rfparameter is provided. To do so, change the line$scriptCmd = {& $wrappedCmd @PSBoundParameters }to:Furthermore, it is recommended to remove the
OutBuffer-related code and to replace allthrowstatements by$PSCmdlet.ThrowTerminatingError($_).In the following full example, I named the proxy function
Remove-ItemProxyand made all the changes I already described. In the end I callSet-Aliasto make it available throughrm. No worries,Set-Aliaswill only affect your current session and won't alter your system permanently. You can copy and paste the following block into your PowerShell profile and whenever you open a PowerShell session, you will be able to userm -rf: