Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'alias' keyword in PowerShell?

Tags:

powershell

Looks like it's not an alias.

get-alias alias error: This command cannot find a matching alias because an alias with the name 'alias' does not exist.

and it's not a command

get-command alias error: The term 'alias' is not recognized as the name of a cmdlet, function, script file, or operable program.

Most probably alias is an alias to get-alias cmdlet, but where is it defined?

like image 666
orad Avatar asked Jan 09 '14 23:01

orad


People also ask

What is an alias in PowerShell?

An alias is an alternate name for a cmdlet, function, executable file, including scripts. PowerShell includes a set of built-in aliases. You can add your own aliases to the current session and to your PowerShell profile.

How do I make an alias in PowerShell?

PowerShell includes built-in aliases that are available in each PowerShell session. The Get-Alias cmdlet displays the aliases available in a PowerShell session. To create an alias, use the cmdlets Set-Alias or New-Alias . In PowerShell 6, to delete an alias, use the Remove-Alias cmdlet.

How do I add an alias to my PowerShell profile?

To create an alias for a command, create a function that includes the command, and then create an alias to the function. To save the aliases from a session and use them in a different session, add the Set-Alias * command to your Windows PowerShell profile.


2 Answers

For some reason, you can omit the get- from a get-xxxx cmdlet in PowerShell and it will add the get- for you.

For instance, you can type hotfix instead of get-hotfix

like image 172
Mike Shepard Avatar answered Oct 20 '22 19:10

Mike Shepard


alias is resolved as Get-Alias, and here's how I found out:

> Trace-Command -Expression { alias } -Name CommandDiscovery -PSHost
DEBUG: CommandDiscovery Information: 0 : Looking up command: alias

<A rather large number of lines deleted, showing exactly where it's looking>

DEBUG: CommandDiscovery Information: 0 : Looking for alias in C:\bin\sysinternals
DEBUG: CommandDiscovery Information: 0 : The command [alias] was not found, trying again with get- prepended
DEBUG: CommandDiscovery Information: 0 : Looking up command: get-alias
DEBUG: CommandDiscovery Information: 0 : Cmdlet found: Get-Alias  Microsoft.PowerShell.Commands.GetAliasCommand

<actual output of get-alias>
like image 26
Eris Avatar answered Oct 20 '22 19:10

Eris