
How to do it…
As we have already mentioned, it is simple to think in PowerShell. When we know that the verb to fetch any information locally is Get, and the noun in this case would be Alias, the cmdlet should be Get-Alias :
- Run Get-Help to understand how to use the cmdlet:
PS> Get-Help Get-Alias
- According to the help documentation, all of the parameters for Get-Alias are optional (they are all enclosed in []). Therefore, simply running Get-Alias will give us a list of all of the aliases that are available in the current instance of PowerShell:

- Now, let's try to resolve the gbp alias to the PowerShell cmdlet that it actually runs in:
PS> Get-Alias gbp
- Now, do the opposite: get the alias for a certain cmdlet. The Definition in the second parameter set of the help documentation should be used. The output shows the actual PowerShell cmdlet that runs when an alias is called:
PS /home/ram> Get-Alias -Definition Get-ChildItem
- We can see two aliases as output, both of which run Get-ChildItem under the hood. Now, let's run dir as well as Get-ChildItem and compare their outputs:
PS> dir
PS> Get-ChildItem
- The two outputs are identical. Now, let's look at what type of object the commands return:
PS> /home/ram> dir | Get-Member
PS> /home/ram> Get-ChildItem | Get-Member
They returned the same object as well.
Now, let's create an alias:
- First, identify a word that you'd like to use as the alias. For example, let's consider listdir.
- Run listdir on PowerShell to ensure that such a cmdlet (or a Linux command) does not already exist.
- List out the cmdlets that deal with aliases by running the following command:
PS> Get-Command *alias
- New-Alias is the cmdlet we are looking for, since it creates a new alias.
- Read the help documentation for New-Alias by running the following command:
PS> Get-Help New-Alias
The help document indicates that only the Name and the Value parameters are mandatory. We will only use these two to create this simple alias.
- Run the following command to create the custom alias:
PS> New-Alias listdir Get-ChildItem
- Check whether the alias was created as desired or not:
PS> Get-Alias listdir
- Next, run the alias to see what output it gives:
PS /home/ram> listdir
That is the output that we are familiar with—the output of Get-ChildItem.
Since aliases are ephemeral, let's export the alias we created. The output of Get-Command a few steps ago showed Export-Alias. Get-Help for the cmdlet shows that there are two ways to export the aliases: as a comma-separated values file, and as a script.
Export the aliases as a CSV file:
PS> Export-Alias aliases.csv
Export the aliases as a script as well:
PS> Export-Alias aliases.ps1 -As Script
View the contents of both files either using cat or Get-Content.
Optionally, edit the file to remove all of the aliases except the ones you created. The custom aliases can be found at the bottom of the list.
Next, import the aliases into your PowerShell session:
- Restart PowerShell.
- Check if the listdir alias that you created exists:
PS> Get-Alias listdir
- Now, import either of the alias exports:
PS> Import-Alias ./aliases.csv
PS> # Or, simply call the script, ./aliases.ps1
If you would like to add these aliases to your session, follow the Enabling automated execution of commands for each load recipe in Chapter 2, Preparing for Administration Using PowerShell.