How to use the command Get-Alias (with examples)
- Windows
- December 25, 2023
Get-Alias is a PowerShell command that allows users to list and retrieve command aliases in the current PowerShell session. An alias is a short name or abbreviation that can be used as a substitute for a command. This command is only available in PowerShell.
Use case 1: List all aliases in the current session
Code:
Get-Alias
Motivation: This use case is useful when you want to see a complete list of all the aliases available in the current PowerShell session. It can help you discover and familiarize yourself with the aliases that are available to use.
Example output:
CommandType Name Version Source
----------- ---- ------- ------
Alias % -> ForEach-Object
Alias ? -> Where-Object
Alias ac -> Add-Content
Alias cat -> Get-Content
Alias cd -> Set-Location
...
Use case 2: Get the aliased command name
Code:
Get-Alias command_alias
Motivation: In some cases, you may come across an alias and want to know the actual command it represents. This use case helps you retrieve the original command name associated with a specific alias.
Example output:
CommandType Name Version Source
----------- ---- ------- ------
Alias cat -> Get-Content
Use case 3: List all aliases assigned to a specific command
Code:
Get-Alias -Definition command
Motivation: When you want to know all the aliases assigned to a specific command, this use case can be handy. It allows you to find all the aliases associated with a particular command efficiently.
Example output:
CommandType Name Version Source
----------- ---- ------- ------
Alias cat -> Get-Content
Alias gc -> Get-Content
Alias type -> Get-Content
Use case 4: List aliases that begin with ‘abc’, excluding those which end at ‘def’
Code:
Get-Alias abc* -Exclude *def
Motivation: This use case is useful when you want to filter aliases based on specific patterns. In this example, it lists all the aliases that begin with ‘abc’ while excluding those that end with ‘def’. It allows you to narrow down your search and focus only on relevant aliases.
Example output:
CommandType Name Version Source
----------- ---- ------- ------
Alias abc1 -> Some-Command
Alias abc2 -> Another-Command