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.

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

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.

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.

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.

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:

eth helpme test

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.

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

[adinserter name=”In Article”]

Now lets import the aliases from the previous session.

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.

[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

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:

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:

[adinserter name=”Matched-Content”]