6 min read
Boost your IT automation efficiency with new ScriptRunner release
We have just released our latest ScriptRunner update, version 7.1, packed with powerful new features aimed at making IT...
ScriptRunner Blog
Aliasing is one of the primary ways to speed up working with any command line.
It can speed up your work in a multitude of ways, keeping you from typing out frequently used commands and creating the occasional typo.
To interact with aliases in PowerShell we use the family of commandlets ending in Alias. You can retrieve them easily by using Get-Help:
-Name and -Value are required and should not contain any spaces, the description is optional but can help you remember why you created this alias.
The Value is not limited to commandlets, you can also use executable files on your disk by providing a full path to them.
When aliasing commandlets you can even use all flags of the aliased commandlet while referencing the alias -tab completion will also work as expected.
If you ever get an error trying to create an alias there is a high probability there is already an alias using that name. To overwrite it you can make use of the -Force switch, even though I would recommend investigating the existing alias before by running Get-Alias .
Additional filtering is possible on multiple criteria, like the -Definition or -Name of the alias. Both
parameters support wildcards, which allows you to, for example, find any alias for Get- commandlets.
Therefore, we want to define our aliases in the profile file. If you want to learn how that works, read the article in MS Documentation.
Inside your profile you can use the same command to create your aliases as in your interactive session.
To get the aliases you may have already created in your session you can just filter your history.
If you want some inspirations on common aliases I collected, check out my personal profile I recently open sourced.
For example, this will not work. There will be no error on creation, but running it PowerShell will not be able to locate the commandlet.
There is a way to work around this, however it is quite limited, as it removes the capability to tab complete or even use any of the other parameters the aliased commandlet can provide.
Unfortunately I am not aware of a way to solve this limitation, if you are, please reach out to me, and I am happy to add it for anyone else interested.
Addendum: Shortly after this article was released, ScriptRunner’s very own Achim Wieser provided one possible solution for this issue, which contains the use of an advanced function as a wrapper.
The most important reason is compatibility, imagine you have an alias lr as described above and use it in your scripts. Anyone else running your script will then be greeted by an error message on running the script and reaching the alias.
Or even worse, there could be a different command behind the alias, breaking the whole script or altering its effects.
Working in VSCode using aliases will, by default, be shown as a warning and the default ms-vscode.powershell extensions even provides an automated way of resolving any alias in your code by opening the command bar and typing PowerShell: Expand Alias.
If you’re interested in more tips on how you can improve your daily work with PowerShell, make sure to have a look at our article about the Top 10 PowerShell Commands for Troubleshooting Windows Problems.
Sep 30, 2024 by Frank Kresse
We have just released our latest ScriptRunner update, version 7.1, packed with powerful new features aimed at making IT...
Aug 16, 2024 by Heiko Brenn
Welcome to Scriptember! We are thrilled to announce the launch of a unique, month-long campaign dedicated to...
Aug 14, 2024 by Jeffery Hicks
I'd like to think that because you are reading this, you are a professional PowerShell user and this article will be a...
Marco is a DevOps engineer at heart and uses PowerShell in combination with other stacks to automate deployment, perform data analytics, and bring enterprises to the cloud. He has been using PowerShell as his primary command line on all platforms since 2018 and is always trying to get more people to switch from bash and others.