PowerShell Core for Linux Administrators Cookbook
上QQ阅读APP看书,第一时间看更新

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 :

  1. Run Get-Help to understand how to use the cmdlet:
PS> Get-Help Get-Alias
If you're unsure about any command, or you would like to reduce keystrokes without involving aliases, use tab-completion. Write a part of the cmdlet or parameter and press the Tab key. PowerShell will complete the command for you, or show you suggestions, based on which platform you're doing this on.

  1. 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:
  1. Now, let's try to resolve the gbp alias to the PowerShell cmdlet that it actually runs in:
PS> Get-Alias gbp
  1. 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

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

  1. First, identify a word that you'd like to use as the alias. For example, let's consider listdir.
  2. Run listdir on PowerShell to ensure that such a cmdlet (or a Linux command) does not already exist.
  3. List out the cmdlets that deal with aliases by running the following command:
PS> Get-Command *alias
  1. New-Alias is the cmdlet we are looking for, since it creates a new alias.
In PowerShell, the Set verb is used to modify something. Therefore, Set-Alias  is used to modify an alias that already exists.
  1. 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.

  1. Run the following command to create the custom alias:
PS> New-Alias listdir Get-ChildItem

  1. Check whether the alias was created as desired or not:
PS> Get-Alias listdir
  1. Next, run the alias to see what output it gives:
PS /home/ram> listdir

That is the output that we are familiar withthe output of Get-ChildItem.

Aliases are ephemeral by default. They exist only as long as your PowerShell session exists. To use custom aliases without having to recreate them each time, export these aliases (the instructions for which are in the next recipe) and import them using your PowerShell profile. We will look at profiles in Chapter 2, Preparing for Administration using PowerShell.

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:

  1. Restart PowerShell.
  2. Check if the listdir alias that you created exists:
PS> Get-Alias listdir
  1. Now, import either of the alias exports:
PS> Import-Alias ./aliases.csv
PS> # Or, simply call the script, ./aliases.ps1
If you did not remove the pre-existing aliases, you may receive several errors, each saying that the new alias could not be created. There are two ways to handle this: the first way is to remove the default aliases from the export file (which is recommended), and the second way is to use the   -Force  parameter (this may still result in errors, but there would be significantly fewer).

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.