1

I want to use ⍋ to sort ascending, and use ⍒ to sort descending.

I successfully set ⍋ as an alias for sort (default is ascending):

Set-Alias -Name ⍋ -Value Sort-Object

I failed to set ⍒ to an alias for sort descending:

Set-Alias -Name ⍒ -Value Sort-Object -Descending

Here is the error message I received:

Set-Alias : A parameter cannot be found that matches parameter name 'Descending'.
    At line:1 char:38
    + Set-Alias -Name ⍒ -Value Sort-Object -Descending
    +                                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Alias], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

Any help would be appreciated.

mklement0
  • 382,024
  • 64
  • 607
  • 775
user3217626
  • 129
  • 1
  • 1
  • 3
  • 1
    you can only set an alias for a cmdlet or function ... not a cmdlet/function with a parameter. you will need to make a wrapper function and alias that. [*grin*] – Lee_Dailey Mar 18 '19 at 21:02
  • Indeed, @Lee_Dailey. Closely related: https://stackoverflow.com/q/2468145/45375 - though if you want the alias to accept _pipeline input_, [rokumaru's answer](https://stackoverflow.com/a/55230247/45375) is the way to go. – mklement0 Mar 18 '19 at 23:00
  • @mklement0 - the `.GetSteppablePipeline()` stuff is new to me ... i'm off for some reading ... [*grin*] – Lee_Dailey Mar 18 '19 at 23:27
  • @Lee_Dailey: See https://blogs.msdn.microsoft.com/powershell/2009/01/04/extending-andor-modifing-commands-with-proxies/ – mklement0 Mar 19 '19 at 01:33

1 Answers1

1

You can not set aliases that include parameters. You need to create a function that wraps the cmdlet.

function Sort-ObjectDescending
{
    [Alias("⍒")]

    param(
        [Parameter(Position = 0)]
        [Object[]]
        $Property,

        [Switch]
        $CaseSensitive,

        [String]
        $Culture,

        [Switch]
        $Unique,

        [Parameter(ValueFromPipeline)]
        [PSObject]
        $InputObject
    )

    begin {
        try {
            $scriptCmd = { Sort-Object -Descending @PSBoundParameters }
            $steppablePipeline = $scriptCmd.GetSteppablePipeline()
            $steppablePipeline.Begin($PSCmdlet)
        } catch {
            throw
        }
    }

    process {
        try { $steppablePipeline.Process($_) } catch { throw }
    }

    end {
        try { $steppablePipeline.End() } catch { throw }
    }
}
rokumaru
  • 1,244
  • 1
  • 8
  • 11