PowerShell Not your Father’s Command Line Part 6 of 31: A Cmdlet By Any Other Name Would Be An Alias

imageOne of the great things about PowerShell cmdlets is you can give them any name you want them to have using a feature called aliases. More importantly there are several built-in aliases in PowerShell allowing you to leverage your command prompt knowledge. If you have ever opened a PowerShell session and typed in cls to clear your screen you actually ran the Clear-Host cmdlet.  Here are some quick notes you need to know about aliases:

  • Allows you to leverage your existing knowledge, even UNIX knowledge (for example try ls)
  • You can assign/create your own aliases to cmdlet, function, script and virtually any thing you want to shorten
  • You can have multiple aliases for the same command
  • You can create your own aliases

To see what aliases you currently have on your system you can run Get-Command, but this will show all of the cmdlets, functions and aliases, to see just the alias run the following cmdlet:

Get-Alias

If you are looking for the particular PowerShell cmdlet an alias is associated you can just specify the alias name at the end of the Get-Alias cmdlet.  For example if you wanted to see the cmdlet behind the cls alias you would run the following command (your results would look similar the following screenshot):

Get-Alias cls

image

Creating aliases is as simple at using one cmdlet Set-Alias (you could also use New-Alias) Set-Alias has multiple functions as well, it will also change existing aliases as well as make new aliases.  So to create an alias called now for the Get-Date cmdlet you would run the following command:

Set-Alias now Get-Date

After you created the alias you can then use it in place of the full cmdlet name, after you have created the now alias you can then simply run type in the name and you get the current date.  One important note when you create an alias you cannot include parameters with the cmdlet.  You have to use just the cmdlet and nothing but the cmdlet. For example you could not make an alias for the following start-service netlogon.  The following command would fail, with an error similar to  “A positional parameter cannot be found that accepts argument” Set-Alias StNet Start-Service Netlogon

The last note I want to mention is that aliases like a few things in PowerShell are not permanent.  Like previously mentioned in other posts, you can use profiles which we will talk about in another post.  You also have the ability to export the aliases (Export-Alias to a text file and import (Import-Alias) them back into a PowerShell session when you need them.  This also makes your aliases somewhat portable.  Just one note, you may see a few errors when you import the alias file back in.  This is due to the fact when you export the aliases you will also get all the predefined aliases as a part of the export process.  There a few ways to fix this the most straight forward way is to simply modify the exported file, all of your custom aliases are listed at the bottom of the file.  If you want to know of some of other secrets or if you know some shortcuts, I will put together another post on exporting and importing aliases.

To end the post here is some fun alias trivia for you:

What is gal?

  1. Global Address List
  2. Guild of American Luthiers  (no I am not making this up: https://www.luth.org/
  3. Common Abbreviation for the book of Galatians,
  4. An Alias for Get-Alias
  5. All of the above

If you guessed #5 All of the above,  you get 10 Bonus points!  Yes there is an alias for Get-Alias, just thought it would be fun to share. Thanks for reading, this was a quick post on aliases, and you will many examples as we go through the 31 days.  This was just to give a quick introduction.

Remember if there is a topic on PowerShell you are curious about and would like Sarah or myself to cover in these postings, all you need to do is ask. Email either of us with your comments or suggestions, we look forward to hearing from you. Have a great day!