How to do it...

  1. First, identify a word that you'd like to use as the alias. For example, let us consider listdir.
  2. Run listdir on PowerShell to ensure such a cmdlet (or a Linux command) does not already exist.
  3. List out the cmdlets that deal with aliases, by running:
Get-Command -Noun Alias
Remember that the nouns in PowerShell are singular. Therefore, there would be no first-party cmdlet that contains Aliases. If third party modules give you Aliases as a noun in them, they are not following the PowerShell best practice of using only singular nouns.
  1. New-Alias is the cmdlet we are looking for, since it creates a new alias. (Set-Alias is used to modify an alias that already exists.)
  2. Read the help documentation for New-Alias by running:
Get-Help New-Alias

The help document indicates that only the Name and the Value parameters are mandatory. We shall use only the two to create this simple alias.

  1. Run the following to create the custom alias:
New-Alias listdir Get-ChildItem
  1. See whether the alias was created as desired or not.
PS> Get-Alias listdir

CommandType Name Version Source
----------- ---- ------- ------
Alias listdir -> Get-ChildItem
  1. Also, run the alias to see what output it gives.
PS /home/ram> listdir

Directory: /home/ram

Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 4/20/18 6:36 AM Desktop
d----- 5/10/18 1:05 PM Documents
d----- 5/16/18 11:03 AM Downloads
d----- 4/20/18 6:36 AM Music
d----- 5/1/18 2:19 PM Pictures
d----- 4/20/18 6:36 AM Public
d----- 4/20/18 6:36 AM Templates
d----- 4/20/18 6:36 AM Videos

That is the output that we are familiar with–the 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 understand profiles in a later chapter.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.54.63