14 min read
How to Use Winget and PowerShell for Efficient App Deployment
Boost IT efficiency with Winget and PowerShell! Learn how to automate app installations, updates, and management...
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.
Dec 19, 2024 by Jeffery Hicks
Boost IT efficiency with Winget and PowerShell! Learn how to automate app installations, updates, and management...
Dec 17, 2024 by Sonny Jamwal
Extend PowerShell with .NET for powerful event automation. Learn how to monitor and handle system events like a pro!...
Dec 17, 2024 by Sonny Jamwal
Optimize event monitoring with PowerShell WMI! Learn how to automate system tasks and streamline event management...
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.