How to do it...

Now that the best practice has been looked at, let us now look at listing out all the aliases in the system. Like already mentioned, it is simple to think in PowerShell. When we know that the verb to fetch any information is Get, and the noun in this case would be Alias, the cmdlet could be Get-Alias.

  1. Running a quick Get-Command on Get-Alias would tell us whether there indeed is such a cmdlet.
Get-Command Get-Alias
  1. Let us now run Get-Help to understand how to use the cmdlet.
Get-Help Get-Alias
If you're unsure about any command, or 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 the parameters for Get-Alias are optional (they are all enclosed in []). Therefore, simply running Get-Alias would give us a list of all the aliases available in the current instance of PowerShell.
  1. Let us now try to resolve the alias, gbp to the PowerShell cmdlet that it actually runs.
PS> Get-Alias gbp

CommandType Name Version Source
----------- ---- ------- ------
Alias gbp -> Get-PSBreakpoint
  1. Let us now look at how to do the opposite: get the alias for a certain cmdlet. If you read the help documentation for this cmdlet, you'd see a parameter called Definition in the second parameter set. This is the actual PowerShell cmdlet that runs when an alias is called.
PS /home/ram> Get-Alias -Definition Get-ChildItem

CommandType Name Version Source
----------- ---- ------- ------
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
  1. We can see two aliases as output, both of which run Get-ChildItem under the hood. Let us now run dir as well as Get-ChildItem and compare their outputs.
PS /home/ram> dir

Directory: /home/ram

Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 06/04/2018 13:05 Desktop
d----- 18/05/2018 16:01 Documents
d----- 18/05/2018 16:01 Downloads
d----- 06/04/2018 13:05 Music
d----- 20/05/2018 02:17 Pictures
d----- 06/04/2018 13:05 Public
d----- 06/04/2018 13:05 Templates
d----- 10/04/2018 03:41 Videos
PS /home/ram> Get-ChildItem

Directory: /home/ram


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 06/04/2018 13:05 Desktop
d----- 18/05/2018 16:01 Documents
d----- 18/05/2018 16:01 Downloads
d----- 06/04/2018 13:05 Music
d----- 20/05/2018 02:20 Pictures
d----- 06/04/2018 13:05 Public
d----- 06/04/2018 13:05 Templates
d----- 10/04/2018 03:41 Videos
  1. The two outputs are identical. Let us now look at what type of object the commands return.
PS /home/ram> dir | Get-Member

TypeName: System.IO.DirectoryInfo

Name MemberType Definition
---- ---------- ----------
LinkType CodeProperty System.String LinkType{get=GetLinkType;}
Mode CodeProperty System.String Mode{get=Mode;}
...
PS /home/ram> Get-ChildItem | Get-Member

TypeName: System.IO.DirectoryInfo

Name MemberType Definition
---- ---------- ----------
LinkType CodeProperty System.String LinkType{get=GetLinkType;}
Mode CodeProperty System.String Mode{get=Mode;}
...

They returned the same object as well.

..................Content has been hidden....................

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