Get-Alias

 

Additional Resources for Get-Alias

 

Listing All Your Windows PowerShell Aliases

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/get-alias.mspx

 

 

SYNOPSIS

Gets the aliases for the current session.

 

SYNTAX

Get-Alias [[-name] <string[]>] [-scope <string>] [-exclude <string[]>] [<CommonParameters>]

 

DETAILED DESCRIPTION

The Get-Alias cmdlet gets the alternate names for cmdlets, functions, and executable files that have been established for the current session. This collection includes built-in aliases, aliases that you have set or imported, and aliases that you have added to your Windows PowerShell profile. If you specify one or more aliases, Get-Alias gets the alias object and displays its properties, including the object that was aliases, such as the full name of a cmdlet. This feature is made available by the Windows PowerShell Alias provider.

 

PARAMETERS

 

-name <string[]>

Specifies the alias to retrieve. By default, Get-Alias retrieves all aliases defined for the current session. The parameter name ("-Name") is optional.

 

Required?

false

Position?

1

Default value

*

Accept pipeline input?  

true (ByValue, ByPropertyName)

Accept wildcard characters? 

true

 

-scope <string>

Specifies the scope in which this alias is valid. Valid values are "Global", "Local", or "Script", or a number relative to the current scope (0 through the number of scopes, where 0 is the current scope and 1 is its parent). "Local" is the default. For more information, type "get-help about_scope".

 

Required?

false

Position?

named

Default value

All locally visible aliases

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-exclude <string[]>

Omits the specified items. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as "s*". Wildcards are permitted.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

true

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

INPUT TYPE

None

 

RETURN TYPE

aliasInfoObject

 

NOTES

 

For more information, type "Get-Help Get-Alias -detailed". For technical information, type "Get-Help Get-Alias -full".

 

An alias is an alternate name or nickname for a cmdlet, function, or an executable file. To run the cmdlet, function, or executable, you can use its full name or any alias. For more information, type "get-help about_alias".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

You can also refer to Get-Alias by its built-in alias, "gal".

 

To create a new alias, use Set-Aliasor New-Alias. To delete an alias, use Remove-Item.

 

EXAMPLE 1

 

get-alias

 

This command retrieves all aliases for the current session. The default display includes the CommandType (always "Alias"), the name of the alias (in the Name column), and the cmdlet that is aliased (in the "Definition" column).

 

EXAMPLE 2

 

get-alias -name g*, s*

 

This command retrieves all aliases that begin with "g" or "s".

 

EXAMPLE 3

 

get-alias | where-object {$_.Definition -match "Get-Childitem"}

 

In its simplest form, the Get-Alias cmdlet retrieves the cmdlet name when you know the alias. However, you can use this command format to find the aliases when you know the cmdlet name.

 

The name of the aliased cmdlet is stored in the Definition property of the alias. So, to find the aliases for a given cmdlet, you search for aliases with a Definition property that matches the cmdlet name.

 

First, the command retrieves all aliases ("get-alias"), and then it pipes the results to the Where-Objectcmdlet. The "{$_.definition -match "get-childitem"}" element tells Where-Object to retrieve only the aliases in which the value of the Definition property is " Get-Childitem".

 

The result is a list of all of the aliases for the Get-Childitem cmdlet.

 

Definition is just one property of the AliasInfo objects that Get-Alias retrieves. To find all properties and methods of AliasInfo objects, type "get-alias | get-member".

 

EXAMPLE 4

 

get-alias | where-object {$_.Options -match "ReadOnly"}

 

This command retrieves all aliases in which the value of the Options property is ReadOnly. This command provides a quick way to find the aliases that are built into Windows PowerShell, because they have the ReadOnly option.

 

First, the command retrieves all aliases ("get-alias"), and then it pipes the results to the Where-Objectcmdlet. The "{$_.Options -match "ReadOnly"}" element tells Where-Object to retrieve only the aliases in which a value of the Options property is "ReadOnly".

 

Options is just one property of the AliasInfo objects that Get-Alias retrieves. To find all properties and methods of AliasInfo objects, type "get-alias | get-member".

 

EXAMPLE 5

 

(get-alias | where-object {$_.Options -match "ReadOnly"}).count

 

This command displays the number of aliases with the ReadOnly option. It saves you from tedious counting and lets you compare sets of objects with different properties.

 

This command is identical to the command in the previous example, except that the previous command is now enclosed within parentheses and is followed by the ".count" property.

 

Windows PowerShell first executes the command within the parentheses. Then, instead of displaying the results, it counts the results and displays the number counted.

 

To count the number of objects retrieved by any command, just enclose the command in parentheses and append ".count."

 

EXAMPLE 6

 

get-alias | out-string -stream | select-string "Get-Command"

 

This command displays aliases that includes the phrase " Get-Command". Unlike the previous command, this one finds the phrase in any property of the alias. It also demonstrates the difference between working with objects and working with strings.

 

The command uses the Get-Alias cmdlet to get a set of AliasInfo objects; one for each alias in the shell.

 

The pipeline operator (|) sends the output to Out-String, which converts the objects to a series of strings. It uses the Stream parameter to send each string individually, instead of a single string. Another pipeline operator sends the strings to Select-String, which selects the strings that contain "Get-Command" anywhere in the string.

 

If you omit the Stream parameter, the command displays all of the aliases, because Select-String finds "Get-Command" in the single string that Out-String returns, and the formatter displays the string as a table.

 

For information about Out-String, type "Get-Help Out-String -detailed".

 

RELATED LINKS

Set-Alias

New-Alias

Export-Alias

Import-Alias