• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Aliases

PowerShell Aliases

03/07/2018 by Stephanos Leave a Comment

PowerShell Aliases

In this tutorial we will see about PowerShell Aliases. In PowerShell you are allowed to use aliases in order to run cmdlets, commands, functions, scripts or executable files. Aliases are words that are predefined or you can define in order to run the above. Aliases that you create are saved only under the current session. We will see later in the tutorial how you are able to use your alias on all sessions and load them each time. It is important to understand that it is a very bad practise to use aliases for scripting. Aliases may perform completely different actions on different profiles or systems. The best practise is to use full cmdlets while scripting to ensure that no errors will occur due to aliases. Aliases are good for working quickly as one off solution but not for long term coding.

Related cmdlets

There are few cmdlets that you need to know in order to see, create, export or import aliases in PowerShell. You will see their use later in the tutorial as we go through the aliases.

Below is the list of related cmdlets:

  • Get-Alias
  • New-Alias
  • Set-Alias
  • Export-Alias
  • Import-Alias 

Built-in Aliases

In PowerShell there are some built-in aliases that are loaded each time you open a PowerShell session. You are able to see the built-in aliases by using Get-Alias  cmdlet.

PowerShell Aliases - Get Alias

[adinserter name=”In Article”]

Find the Aliases

As I have mentioned above we are able to see the built-in aliases by using Get-Alias cmdlet. Using the same, you are able to see also your custom aliases if you have created or imported any. There is another way to see the aliases available in the current session, by accessing the alias provider. In order to access the alias provided you need to use the below Set-Location Alias or cd Alias. Then you are able to check the aliases as files by using the Get-ChildItem cmdlet to see them. Check below screenshot.

PowerShell Aliases - Alias Provider

Create Alias

Except from the built-in aliases that PowerShell has, as we have mentioned above, you are able also to create your own ones. In order to create an alias you need to use New-Alias  or Set-Alias  cmdlets. Please note that Set-Alias is also able to modify an alias. If you use Set-Alias to create your own alias and the alias exists, then this will modify the existing alias. If the alias that has the same name is read-only you will not able to modify it unless you use Force.

In the example below I will create an alias to provide me with the Network Adapters on the PC. I will use “eth” as my alias for Get-NetAdapter. As you can see from the screen shot below, the alias does not exists.

PowerShell Aliases - No Alias

Now I will create the alias by using the following command:

New-Alias -Name 'eth' -Value Get-NetAdapter

In the screen shot below you can see that if I run Get-NetAdapter or use my alias “eth”, I get the same result, as I actually run the same cmdlet.

PowerShell Aliases - New Alias

Note that if I close and reopen the session the alias will not exist as the alias that I have created will be kept only for the current session.

[adinserter name=”In Article”]

By creating an alias for a cmdlet, you can also use the options and parameters of that cmdlets the same way as you are using the cmdlet itself.

PowerShell Aliases - New Alias with option

For more information about New-Alias, please check the related link below as there are few options that you can specify when you are creating an alias.

Save Aliases

In order to be able to use your aliases for other sessions or every time that you load Powershell, there are two ways to do it:

  • Save them under profile
  • Export and Import aliases
Save under profile

If you save the aliases under your profile you will not need to import them every time you run PowerShell. They will be directly loaded once you run it. If you save them under Current User profile then only you will be able to use them. If you save them under all users profile, all user will be able to use them when they load PowerShell. More discussion about PowerShell profile on another tutorial.

Export and Import aliases

The other way is to export the aliases in a file and import them whenever you need them. In order to do so you need to use Export-Alias to export the aliases in a file and then you need to use Import-Alias in the session that you need to load those aliases. In the example below, I have created 2 more aliases in order to import them later. So now in the current session we have three aliases:

  1. eth
  2. helpme
  3. test

PowerShell Aliases - Created Aliases

Code:

Export-Alias C:\Scripts\myaliases.csv

 

[adinserter name=”In Article”]

When I exported the aliases, a CSV file has been created that includes all aliases from the current session.

PowerShell Aliases - Exported Aliases

I closed the session and open it again and as you can see in the screenshot below the aliases do not exists.

PowerShell Aliases - Not Imported

 

[adinserter name=”In Article”]

Now lets import the aliases from the previous session.

PowerShell Aliases - Import Alias

The aliases have been imported now. Do not be afraid of all those errors. As you saw above, the file includes also the built-in aliases. Those errors tell us that we are not able to add those aliases. For security purposes, from PowerShell 3.0 and above built-in aliases are not able to be modified except if you use Force option. Of course you can modify the file to include only your custom aliases and not errors will be shown to you. As you can see after I have imported the aliases, my 3 custom aliases have been added in the session and I am now able to use them.

PowerShell Aliases - Confirm Aliases

[adinserter name=”In Article”]

Remove Alias

You are able also to remove an alias from the current session if you do not need it. In order to remove the alias you need to use Remove-Item. As we said above you can access the Alias provider and handle the aliases as files. So in the example below I have removed “test” alias from my current session by using the below command:

Code:

Remove-Item alias:\test

 

PowerShell Aliases - Remove Alias

Create Alias for functions or commands with parameters

Above, we have discussed on how we are able to create an alias of a cmdlet without using any parameters of that cmdlet. Although we have the availability to use the default parameters of that cmdlet. Now we will see how we are able to create an alias using the same cmdlet, Get-NetAdapter, but also include some parameters so that always we will use them as they are.

By default, Get-NetAdapter provide both physical and logical adapters. You have the option to use Physical parameter in order to provide you only with the physical adapters of the system. I want to create an alias in order to use this option by default and get my result accordingly. In order to achieve this you need first to create your function and inside it you need to use the cmdlet including the parameters that you need. After the function is ready we create an alias for that function. Below is the commands that I have used and a screenshot as a proof of the alias that has been created.

Code:

function Get-PhysicalAdapters {Get-NetAdapter -Physical}
Set-Alias eth -Value Get-PhysicalAdapters

Screenshot:

PowerShell Aliases - Alias Function

It does not matter what you will have inside your function. You can use a whole series of commands inside the functions and create an alias for it in order to call the function for future use.

I hope the tutorial about PowerShell Aliases is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Aliases | Microsoft Docs
  • Get-Alias – Microsoft Docs
  • New-Alias – Microsoft Docs
  • Set-Alias – Microsoft Docs
  • Export-Alias – Microsoft Docs
  • Import-Alias – Microsoft Docs
  • Remove-Item – Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Aliases
Article Name
PowerShell Aliases
Description
PowerShell Aliases. In this tutorial you will find information about PowerShell Aliases, how to create them and use them.
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Export-Alias, Get-Alias, Get-ChildItem, Import-Alias, New-Alias, Remove-Item, Set-Alias

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact