Note: The function definition in your question is technically correct, but the error message suggests you used a different function definition - see the bottom section. The first section contains general information.
Unlike in POSIX-compatible shells such as bash
, you cannot include pass-through arguments in a PowerShell alias definition - you indeed need a function for that.
PowerShell aliases - by design - are simply alternative names for other commands, with no ability to "bake in" arguments - for more information about aliases vs. function in PowerShell, see this answer.
Therefore:
# Defines a 'glo' command that executes `git log --oneline` with
# additional arguments, if any, passed through.
function glo { git log --oneline @args }
Note the use of @args
rather than $args
, which is an instance of parameter splatting, which with an array variable passes the array's elements as individual arguments to the target command.
Since you're calling an external program (git
), this isn't strictly necessary, because $args
would do that too; if the target command were a PowerShell command, however, only @args
would pass all arguments through individually.
Note: The function also works if you pass no arguments, in which case git log --oneline
is executed.
For a more sophisticated way of wrapping commands, using proxy functions, see this answer.
To make such "aliases" (wrapper functions) available in all future sessions, add them to your $PROFILE
file.
As for what you tried:
The function definition in your question - function glo { git log --oneline }
- is actually correct, assuming you don't need support for additional, ad hoc pass-through arguments when calling glo
.
However, the error message you're showing implies that you instead defined your function as follows:
# !! BROKEN
function glo { & 'git log --oneline' }
This cannot be expected to work, because &
, the call operator, expects a command name or executable path only as its first argument - any arguments must be passed separately.
If you wanted to use &
in the function definition - which isn't necessary, given that command name git
neither has spaces nor is expressed in terms of variables - define the function body as:
& git log --oneline