In PowerShell, is it possible to create an alias whose name has a space in it?

powershell alias list
powershell permanent alias
powershell alias with parameters
powershell delete alias
powershell alias cd
find all the alias that have been set in your system powershell
powershell ssh alias
powershell alias multiple commands

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.

New-Alias, The New-Alias cmdlet creates a new alias in the current PowerShell session. You can use the Export-Alias cmdlet to save your alias information to a file. New-Alias [-Name] <String> [-Value] <String> [-Description <String>] [-Option None: The alias has no constraints (default value); ReadOnly: The alias can be deleted  You cannot create an alias for a command with parameters and values, such as Set-Location -Path C:\Windows\System32. To create an alias for a command, create a function that includes the command, and then create an alias to the function. For more information, see about_Functions. PowerShell.

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

Set-Alias, An alias is an alternate name that refers to a cmdlet or command. For example A cmdlet can have multiple aliases, but an alias can only be associated with one cmdlet. This command creates an alias to a cmdlet in the current PowerShell session. If the description includes spaces, enclose it single quotation marks. If you create an environment variable in a DOS window and then close that window, you destroy the variable. PowerShell aliases behave in the same way. The solution is to add the alias to your PowerShell profile. Before you add aliases to your profile, however, be selective.

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

    #>
}

Windows 7 and Vista Guide to Scripting, Automation, and Command , Normally, spaces delimit the command name and the arguments, but you can use an unusual twist, to type a program name that has spaces in its name or path, PowerShell first looks through its alias list, which is a list of shorthand names for commands. For example, cd is set up as the alias for the SetLocation cmdlet. An alias is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. You can use the alias instead of the command name in any PowerShell commands. To create an alias, use the New-Alias cmdlet.

The Tips and Tricks Guide to Windows Server 2008 Features, The downside of these command names is that they can be long. So PowerShell has a system for aliases, which are simply nicknames for a command​. The alias is simply a way of shortening the command name or making the cmdlet The parameter name is followed by a space and then whatever value goes with the  PowerShell cmdlets can have aliases. For example, {dir, ls, gci} are all the same as Get-ChildItem. List All Aliases. Get-Alias → show all aliases; Get-Alias *r → show aliases ending in r (Get-Alias has alias gal) Get-Alias Find Fullname. Get-Alias alias → find the fullname of a alias alias. For example, Get-Alias dir

Windows 10 Inside Out (includes Current Book Service), You can create your own aliases to simplify the typing of PowerShell by a space and consists of a hyphen connected to the parameter's name followed by a​  To create or display a variable name that includes spaces or special characters, enclose the variable name with the curly braces ({}) characters. The curly braces direct PowerShell to interpret the variable name's characters as literals.

Windows 10 Inside Out, You can create your own aliases to simplify the typing of PowerShell by a space and consists of a hyphen connected to the parameter's name followed by a​  The Get-Alias cmdlet gets the aliases in the current session. This includes built-in aliases, aliases that you have set or imported, and aliases that you have added to your PowerShell profile. By default, Get-Alias takes an alias and returns the command name. When you use the Definition parameter, Get-Alias takes a command name and returns its aliases. Beginning in Windows PowerShell 3.0, Get

Comments
  • Related: stackoverflow.com/q/4166370/2060966
  • Yes - this or to create a proxy function named rm with a custom parameter rf that corresponds to -Recurse -Force.
  • @notjustme Looking forward to seeing your answer.
  • If I'm not too busy I'll look into it during my lunch break.
  • FYI, I was too busy last Friday but I've posted a working example now.
  • You are correct that this works - however, since the original poster keeps typing rm -rf out of habit I fail to see how being forced to use the call operator will help. Also, & 'rm -rf' \\PathToYourFileWhichYouWantToDelete\FileName.extension wouldn't actually be the equivalent in *nix in this case.
  • Many times you can execute a command by just typing its name, but this will only run if the command is in the environment path. Also if the command (or the path) contains a space then this will fail. Surrounding a command with quotes will make PowerShell treat it as a string, so in addition to quotes, use the & call operator to force PowerShell to treat the string as a command to be executed. Hence forcing the call operator.
  • Technically, this is the correct answer to the question (if it helps OP's original problem or not).
  • @marsze I'm not arguing with that. I guess one could say the OP asked the wrong question. ;)
  • I've looked a bit into PowerShell proxy functions as I've never used them before. It was a good hint. You should probably include some info on how to create such a function in your answer. I still think it's a bit overkill for the original problem (which basically just comes from being used to other shells), but nonetheless the most sophisticated workaround. Thanks for making me learn about proxy functions!
  • Agreed that it might be overkill but it's a "great fit" using a proxy function for the particular scenario nonetheless. Shay Levy has a pretty good article on proxy functions over at The Scripting Guy's blog, even if it's a lil bit dated now.
  • Yea, that's the one I read. I hope there will be some day I can make use of this knowledge. I doubt it though.