I want to make rm -rf
an alias for Remove-Item
, since I keep accidentally typing it when using PowerShell.
I had guessed maybe I could do something like this, but that doesn't work.
Set-Alias -name 'rm -rf' -value Remove-Item
I want to make rm -rf
an alias for Remove-Item
, since I keep accidentally typing it when using PowerShell.
I had guessed maybe I could do something like this, but that doesn't work.
Set-Alias -name 'rm -rf' -value Remove-Item
You could also remove the default alias and then replace it with a custom function.
# remove default alias
if (Test-Path Alias:rm) {
Remove-Item Alias:rm
}
# custom function for 'rm'
function rm {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[switch]$rf,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path
)
process {
Remove-Item -Path $Path -Recurse:$rf -Force:$rf
}
}
Then call it like this:
rm -rf "C:\Temp\dir"
If course, so far this function doesn't have the full functionality of Remove-Item
, but you can extend it as you like.
Note: Even though this "solves" your problem in the short-run, you should not revert to these kinds of workarounds. Better get accustomed to the actual PowerShell commands and syntax, or you're bound to run into more problems sooner or later.
You already have a solution to your problem but as I mentioned, a proxy function could be suitable in this particular scenario. Here's a working example (atleast for PSVersion 5.1).
Adding the following to your $profile should work and you'd be able to run rm -rf "path"
to recursively and forcefully remove a directory. Bear in mind that this hasn't been tested extensively but it does take into account wether you specified -rf or not on the command line. It also supports the common parameters such as -Confirm:$true
.
if(Test-Path Alias:rm) { Remove-Item Alias:rm }
function rm
{
[CmdletBinding(DefaultParameterSetName='Path', SupportsShouldProcess=$true, ConfirmImpact='Medium', SupportsTransactions=$true, HelpUri='https://go.microsoft.com/fwlink/?LinkID=113373')]
param(
[Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string[]]
${Path},
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath')]
[string[]]
${LiteralPath},
[string]
${Filter},
[string[]]
${Include},
[string[]]
${Exclude},
[switch]
${Recurse},
[switch]
${Force},
[switch]
${rf},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[pscredential]
[System.Management.Automation.CredentialAttribute()]
${Credential})
begin
{
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
if($rf)
{
$PSBoundParameters.Remove('rf') | Out-Null
$PSBoundParameters.Add('Recurse', $true) | Out-Null
$PSBoundParameters.Add('Force', $true) | Out-Null
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Remove-Item', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process
{
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end
{
try {
$steppablePipeline.End()
} catch {
throw
}
}
<#
.ForwardHelpTargetName Microsoft.PowerShell.Management\Remove-Item
.ForwardHelpCategory Cmdlet
#>
}
It will work. Set the alias as below.
Set-Alias -Name 'rm -rf' -Value Remove-Item
For calling it, you can use the call operator (&) operator like this -
& 'rm -rf' \\PathToYourFileWhichYouWantToDelete\FileName.extension