Skip to content

Support "Set-Alias" to include parameters (or a full macros replaced before execution) #15855

Closed

Description

@liang2zhu1

Summary of the new feature / enhancement

Summary - Could we get Set-Alias to support what Linux does just replace the alias with the text before execution? There's workaround without this in some case but not always.

Powershell does not support set alias with parameters - eg

PS C:\Users\liangzhu> set-alias -Name topone "Select-Object -First 1"
PS C:\Users\liangzhu> 1,2,3 | topone
topone: The term 'Select-Object -First 1' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

There's one workaround I can do using function

PS C:\Users\liangzhu> function toponef {$input | Select-Object -First 1}
PS C:\Users\liangzhu> 1,2,3 |toponef
1

However this brings a performance penalty in the case of pipeline input - if I run dir c:\ -s | topeonef it will get to wait for the base stream completes (Compare to that - dir c:\ -s | Selelct-Object -First 1 works just fine )

It took me a while to search for an solution (https://stackoverflow.com/a/34832628) yet it has side effects with an error output with base stream (works with a simply array but not complicated like dir) and indeed involves too much into powershell implementation internals that I'm afraid many beginner/intermediate user won't understand yet the scenario is just super common in day-to-day business.

PS C:\Users\liangzhu> Filter TerminateAfterOne {
>>   $sp = { Select-Object -First 1 }.GetSteppablePipeline($MyInvocation.CommandOrigin)
>>   $sp.Begin($true)
>>   $sp.Process(0)
>> }
PS C:\Users\liangzhu> function toponef { begin {$c = 0} process {if (++$c -gt 1) {toponeFilter} else {$_}}}
PS C:\Users\liangzhu> dir c:\ -s | toponef

    Directory: C:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
da---           5/19/2018 12:58 PM                _vNextUpgrade
Get-ChildItem: System error.

Proposed technical implementation details (optional)

No response

Activity

  1. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Aug 1, 2021
  2. iSazonov commented on Aug 2, 2021

    @iSazonov
    Collaborator

    Dup #12962

  3. ghost closed this as completedon Aug 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-DuplicateThe issue is a duplicate.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions