Please feel free to provide feedback or file bugs here.

aliases should be able to contain parameters

While PowerShell's introduction of aliases to Windows command lines is a (long due) help, they are very limited. Compared to aliases found in Linux shells such as bash, aliases in PowerShell have one glaring weakness: they can only replace a single token with another single token.

For example, on Linux, I can do the following:
# Add default parameters to ls so it shows hidden files and uses color
alias ls='ls -A --color=auto'
# Add an alias that shows ls long-form output and file type indicators
alias ll="ls -lF"

On Po9werShell, I can't do anything like that. "ls" is aliased to Get-ChildItem, but gci doesn't show hidden files by default, and *I cannot make an alias that does!*

# Create an alias that lists all contents (this command works)
Set-Alias lsa 'Get-ChildItem -Force'
# Try using that alias though...
lsa
lsa : The term 'Get-ChildItem -Force' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ lsa
+ ~~~
+ CategoryInfo : ObjectNotFound: (Get-ChildItem -Force:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

See https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.utility/Set-Alias. Yes, I can create a function and then alias the function, but that's way more complication and hassle than the way other shells implement aliases.

1 vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)

    We’ll send you updates on this idea

    CBHacking shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • CBHacking commented  ·   ·  Flag as inappropriate

        Happy to, Joey!

        Aside from the relatively minor complaints of "it's more complicated to remember and more to type" (CLIs are supposed to be productive and avoid needless verbosity), and "it's different from the way every other shell implements aliases" (why? One of the great joys of Powershell for multi-platform users is not needing to remember to type "dir" instead of "ls"), it also doesn't support additional parameters or chaining. That's the real blocker.

        Let's go back to that the `ls`, `ll` example above. The expansion of `ll` is defined in terms of `ls`; merely typing `ll` is equivalent to not just `ls -lF`, but actually to `ls -A --color=auto -lF`, because the aliases are expanded recursively. That can be a huge time-saver when creating and reusing complicated commands, and if I later go and change the alias for `ls`, `ll` sees that change automatically.

        Then there's the problem of what if I want to pass additional parameters to my aliases? `ls ..` (list children of parent) works in both Powershell and bash just like I expect it to. `lsa ..` does *not* work correctly in Powershell; as you've defined `lsa` it completely ignores parameters. I can make it work for that case, by redefining `function lsa {Get-ChildItem -Force $args}` (making what should be a simple alias definition even *more* verbose and complicated to remember), but then passing *named* parameters fails, because `lsa` doesn't know that it's supposed to take the parameter list of `Get-ChildItem`; `lsa -recurse` (or simply `lsa -r`) just complains that it can't find a child path "-recurse" to list! Fixing _that_ would require a truly tedious amount of writing out parameter lists on my "alias" functions...

        Contrast that with my bash aliases, where if I also want to sort by time modified I can do `ls -t` or `ll -t`, the latter of which expands to `ls -A --color=auto -lF -t`. Heck, I could make an `llt` alias - `alias llt='ll -t'` - and it would just work the way I expect.

        It's simple, intuitive, takes very few keystrokes, doesn't break if I forget to include `$args` on the tail of every level down the stack, doesn't explode if the parameters are identified by name (or require a super-long list of formal parameter definitions at each level of wrapping), and is the way every other alias-supporting shell that I'm aware of works.

      Feedback and Knowledge Base