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.. 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?
A cmdlet is a command that does stuff for you. For Example Get-Command is one. It's written in C# and compiled (so if you want to read what it does, you need to decompile it, which is often not quite legit). An Alias is an alternative name for a cmdlet or function.
The PowerShell Alias provider lets you get, add, change, clear, and delete aliases in PowerShell. An alias is an alternate name for a cmdlet, function, executable file, including scripts. PowerShell includes a set of built-in aliases. You can add your own aliases to the current session and to your PowerShell profile.
Cmdlets are written in a compiled . NET language, while functions (and scripts) are written in the PowerShell language. On the plus side, this makes certain developer tasks (such as P/Invoke calls, working with generics) much easier in a cmdlet.
From OSP dictionary we would define Function alias as the rules that promote the usage of the function (Java/SQL) to the users (non-developers ) who maintain business rules (Decision tree, Decision table, When) or create/configure reports in Production.
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.
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With