在PowerShell中,是否可以创建名称中带有空格的别名?

时间:2018-11-01 06:12:58

标签: powershell

我想将rm -rf用作Remove-Item的别名,因为在使用PowerShell时我不小心输入了它。

我猜想也许我可以做这样的事情,但这是行不通的。

Set-Alias -name 'rm -rf' -value Remove-Item

3 个答案:

答案 0 :(得分:3)

您还可以remove the default alias,然后将其替换为自定义函数。

# 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
    }
}

然后这样称呼它:

rm -rf "C:\Temp\dir"

如果可以的话,到目前为止,该功能还没有Remove-Item的全部功能,但是您可以随意扩展它。

注意:即使这可以在短期内“解决”您的问题,但您也不应该恢复到此类解决方法。最好习惯实际的PowerShell命令和语法,否则您迟早会遇到更多问题。

答案 1 :(得分:2)

它将起作用。设置别名如下。

Set-Alias -Name 'rm -rf' -Value Remove-Item

要调用它,您可以像这样使用呼叫运算符(&)运算符-

& 'rm -rf' \\PathToYourFileWhichYouWantToDelete\FileName.extension

答案 2 :(得分:2)

您已经有解决问题的方法,但是正如我所提到的,代理功能可能适合这种特定情况。这是一个工作示例(至少适用于PSVersion 5.1)。

将以下内容添加到$ profile中应该可以,并且您可以运行rm -rf "path"来递归并强制删除目录。请记住,尚未对此进行广泛的测试,但是无论您在命令行上是否指定了-rf,它都将考虑在内。它还支持诸如-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

    #>
}