• Blog
  • Webinars
  • Partner
  • Support
  • Contact
  • EN
    • DE
ScriptRunner
  • About us
    • Team
    • Jobs
    • Press
  • Why
  • Use Cases
  • Software
    • ScriptRunner Server
    • ScriptRunner Portal
    • ScriptRunner Portal Widget
    • ScriptRunner Connectors
    • ScriptRunner ActionPacks
  • Try Now
  • Search
  • Menu Menu
You are here: Home1 / ScriptRunner Blog2 / News3 / PowerShell Aliasing

PowerShell Aliasing

Author: Marco Kamner | Reading time: 6 minutes | Category: News, Scripting

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:

Table of content

  • Creating Aliases
  • Viewing Aliases
  • Persisting Aliases
  • Limitations
  • When Not To Use Aliases
  • Conclusion
  • Related links
Article image: PowerShell Aliasing

Creating Aliases

To create a new alias we use the commandlet New-Alias.

-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 .

Viewing Aliases

PowerShell comes with many aliases already enabled, you can view them by executing Get-Alias – This will show you all aliases enabled in the current session.

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.

Persisting Aliases

Everything we did up to now will vanish once you close your current PowerShell session, as setting aliases is not a permanent operation.

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.

Limitations

Aliases sadly have one glaring limitation, you can not use the parmameters of the commandlet you are aliasing inside the value of your commandlet.

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.

Hi @ProfessorLogout , I just read your blog about #PowerShell aliases. I think you can easily resolve the limitations concerning argument completion by using and advanced function as wrapper. What do you think about this approach? https://t.co/xT1ibA9d3w pic.twitter.com/Qnla7UYit6

— Achim Wieser (@AchimWieser) December 2, 2020

You can find a more detailed description of his approach on Marco Kamners Blog.

When Not To Use Aliases

Aliases are a great way to speed up your day-to-day work, however they should not be used in scripts meant for long time usage or distribution.

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.

Conclusion

As this article shows, using PowerShell aliases is quite simple and a good way to save time when processing recurring and repetitive tasks. Similarly, you could use aliases to help you remember certain cmdlets and make it easier to work with your personal use cases.

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.

Related links

  • about_Aliases – PowerShell | Microsoft Docs
  • about_Profiles – PowerShell | Microsoft Docs
  • Marco Kamner · GitLab
Share this article
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
  • Share on Reddit
  • Share by Mail

These articles might also be interesting for you:

Article: PowerShell PSDefaultParameterValuesScriptRunner Software GmbH

PowerShell PSDefaultParameterValues

24. March 2021
Read more
https://www.scriptrunner.com/wp-content/uploads/2021/03/Article-PSDefaultParameterValues.png 449 449 Marco Kamner https://www.scriptrunner.com/wp-content/uploads/2018/05/ScriptRunner_Logo_RGB-300x45.png Marco Kamner2021-03-24 14:10:142021-03-25 13:07:29PowerShell PSDefaultParameterValues
Artikelbild:ScriptRunner Software GmbH

5 PowerShell Scripting Best Practices – From Runnable to Professional Code

10. March 2021
Read more
https://www.scriptrunner.com/wp-content/uploads/2021/03/powershell-best-practices.png 1000 1000 Philip Lorenz https://www.scriptrunner.com/wp-content/uploads/2018/05/ScriptRunner_Logo_RGB-300x45.png Philip Lorenz2021-03-10 10:00:042021-03-10 15:22:175 PowerShell Scripting Best Practices – From Runnable to Professional Code
Article: Introducing Three Popular PowerShell Editors - Adam BertramScriptRunner Software GmbH

Software Spotlight: Introducing Three Popular PowerShell Editors

10. February 2021
Read more
https://www.scriptrunner.com/wp-content/uploads/2021/02/powershell-editors.gif 1000 1000 Adam Bertram https://www.scriptrunner.com/wp-content/uploads/2018/05/ScriptRunner_Logo_RGB-300x45.png Adam Bertram2021-02-10 10:00:112021-02-25 12:41:38Software Spotlight: Introducing Three Popular PowerShell Editors

About the author:

Portrait des ScriptRunner Autors Marco Kamner
Marco Kamner

Marco is a DevOps Engineer at heart, using PowerShell in combination with other stacks to automate deployments, data analytics and get companies to the cloud. He uses PowerShell as his primary command line since 2018 on all platforms and always tries to get more people to switch from bash and others.

Latest posts:

  • Article: PowerShell PSDefaultParameterValuesScriptRunner Software GmbHPowerShell PSDefaultParameterValues24. March 2021 - 14:10
  • Article: ScriptRunner is sponsoring the PowerShell + DevOps Global Summit 2021ScriptRunner Software GmbHScriptRunner is sponsoring the PowerShell + DevOps Global Summit 202119. March 2021 - 10:00
  • ScriptRunner ActionPack for CitrixScriptRunner Software GmbHScriptRunner ActionPack for Citrix11. March 2021 - 13:02
  • Artikelbild:ScriptRunner Software GmbH5 PowerShell Scripting Best Practices – From Runnable to Professional Code10. March 2021 - 10:00
  • Artikelbild: Vorschau auf das Produktjahr 2021ScriptRunner Software GmbHPreview of the product year 20219. March 2021 - 10:00

Product

  • ScriptRunner Platform
  • ScriptRunner Server
  • ScriptRunner Portal
  • ScriptRunner Portal Widget
  • ScriptRunner Apps
  • ScriptRunner Connectors
  • Script Collections
  • Licensing
Get your free trial

Solutions

  • IT Administrators
  • IT Team Leaders
  • Use Cases

Resources

  • Blog
  • Documentation
  • Knowledge Base
  • Webinars
  • PowerShell Lexicon
  • PowerShell Poster
  • PowerShell Security Ebook

Company

  • About us
  • Team
  • Jobs
  • Press
  • References
  • Partner

Contact

ScriptRunner Software GmbH
Ludwig-Erhard-Straße 2
76275 Ettlingen
Germany

T: +49 7243 20715-0
M: info(at)scriptrunner.com

Request Demo
© ScriptRunner Software GmbH is a subsidiary of AppSphere AG
  • LinkedIn
  • Xing
  • Twitter
  • Facebook
  • Youtube
  • Imprint
  • Privacy Policy
  • Newsletter
The new ScriptRunner Portal – three becomes one Article: The new role-based ScriptRunner portalScriptRunner Software GmbH Article: An Introduction to PowerShell in Citrix Virtual Apps and DesktopsScriptRunner Software GmbH An Introduction to PowerShell in Citrix Virtual Apps and Desktops
Scroll to top