Repository navigation
Description
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
iSazonov commentedon Aug 2, 2021
Dup #12962