7

Im setting up my powershell profile to create aliases of commonly used commands. On Microsoft's documentation it says, if I want to make an alias for a command with parameters, I should make the value of the alias a Function that does that.enter image description here. However, when I type the name of the function in the command line it works just as well as an alias.

In other words, in the above picture, if I typed CD32 it would behave the same as if I typed Go in the command line

So my question is: Why do I use aliases pointing to functions when I could just have a function? Are there feature differences between the two?

nreh
  • 476
  • 8
  • 13
  • 3
    What that documentation is saying is that if you want to create an "alias" for a command and specify parameter values as part of the "alias", you must use a function. You can certainly create an alias for a command that takes parameters, and you can specify those parameters when using the alias, but you cannot fix any of the parameters in place for the alias. The alias is just an alias for the command, not for command-with-specific-values. For that you need to use a function. – Lasse V. Karlsen Aug 06 '20 at 21:03
  • 3
    Let me rephrase. The `CD32` function above, since it wants to pass the parameter `C:\Windows\System32` to `Set-Location`, a function has to be made. If you simply want to make an alias for `Set-Location` you can use an alias, but then it will function exactly like `Set-Location`, and so when you use the alias you also has to tell it which location to set. Does any of this makes sense ? – Lasse V. Karlsen Aug 06 '20 at 21:04
  • 3
    Ahh that makes sense. So an alias acts just like the command it is pointing to, including how you give it parameters. It's kind of a shorthand of writing a command. – nreh Aug 06 '20 at 21:16
  • 1
    Exactly. That was ... quite a bit clearer than what I wrote :) – Lasse V. Karlsen Aug 07 '20 at 12:00

1 Answers1

10
  • An alias in PowerShell allows you to define an alternative name for another command.

    • Unlike in POSIX-compatible shells such as bash, you cannot include pass-through arguments in its definition - you need a function for that (see below).

    • The typical use case is to define a short alternative name for convenience of interactive invocation; for instance, PowerShell has a built in gc alias for its Get-Content cmdlet. PowerShell even recommends a naming convention for aliases, based on official short alias prefixes for its approved verbs, such as the g for the Get verb in the given example.

    • Another, problematic use is to define aliases named for a different shell's commands; for instance, PowerShell has a built in dir alias for its Get-ChildItem, named for cmd.exe's (Command Prompt's) internal dir command. While that may be somewhat helpful while transitioning from cmd.exe, it only works in very simple invocations, and quickly becomes problematic due to PowerShell's fundamentally different command-line syntax and differing parameter names.

    • Another, unproblematic use is to define an alias for an external executable whose directory isn't listed in the path ($env:PATH); e.g., if you want to execute c:\path\to\foo.exe as just foo without having to add c:\path\to to $env:PATH, you can use Set-Alias foo c:\path\to\foo.exe.

    • Unlike in POSIX-compatible shells such as bash, aliases are (invariably) usable in scripts (*.ps1 files), but their use in scripts is discouraged in the interest of robustness and long-term stability.

  • A function, as is to be expected, is a named unit of code that can accept arguments and can perform arbitrary operations.

    • A function is what you need to use if you want to wrap existing commands, by hard-coding pass-through arguments and / or providing custom logic around the invocation of the wrapped command - see this answer.

As for whether it makes sense to define an alias for a function, if implementation of your command requires a function (due to requiring more than just a simple name mapping):

  • If all you need is one (short) name for your command, you can define your function directly with that name - no alias needed.

  • By contrast, if your function needs a long name, especially an advanced (cmdlet-like) function that adheres to PowerShell's verb-noun naming convention (e.g., Invoke-Foo), and you also want a short name for interactive convenience (e.g., foo), you'll have to also define an alias for that function with that short name (e.g., Set-Alias foo Invoke-Foo).

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • an `alias` works with parameters in one occasion: `set-alias a get-alias`, then `a foo` will work if you have `set-alias foo hi`. result: `hi(story)` because `hi` is a ps alias. – Timo May 13 '21 at 14:30
  • 2
    I don't understand your example, @Timo. Obviously you can pass parameters (arguments) _to already defined aliases_ on _invocation_, and aliases can even refer to other aliases, but the point is that you cannot "bake" predefined arguments into an alias _definition_, because an alias is simply another name for another _command_, and not for a command _plus predefined arguments_. – mklement0 May 13 '21 at 14:36
  • 1
    Ok, there is no alias body in contrast to function body. – Timo May 13 '21 at 15:03