Set-Alias

 

Additional Resources for Set-Alias

 

Remapping an Existing Windows PowerShell Alias

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

 

 

SYNOPSIS

Creates or changes an alias (alternate name) for a cmdlet or other command element in the current Windows PowerShell session.

 

SYNTAX

Set-Alias [-name] <string> [-value] <string> [-description <string>] [-option {<None> | <ReadOnly> | <Constant> | <Private> | <AllScope>}] [-passThru] [-scope <string>] [-force] [-whatIf] [-confirm] [<CommonParameters>]

 

DETAILED DESCRIPTION

The Set-Alias cmdlet creates or changes an alias (alternate name) for a cmdlet or for a command element, such as a function, a script, a file, or other executable. You can also use Set-Alias to reassign a current alias to a new command, or to change any of properties of an alias, such as its description. Unless you add the alias to the Windows PowerShell profile, the changes to an alias are lost when you exit the session or close Windows PowerShell.

 

PARAMETERS

 

-name <string>

Specifies the new alias. You can use any alphanumeric characters in an alias, but the first character cannot be a number.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-value <string>

Specifies the name of the cmdlet or command element that is being aliased.

 

Required?

true

Position?

2

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-description <string>

Specifies a description of the alias. You can type any string. If the description includes spaces, enclose it quotation marks.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-option <ScopedItemOptions>

Sets the value of the Options property of the alias.

 

Valid values are:

 

·          None: Sets no options. (default)

·          ReadOnly: The properties of the alias cannot be changed, except by using the Force parameter. You can use Remove-Item to delete the alias.

·          Constant: The alias cannot be deleted and its properties cannot be changed. Constant is available only when you are creating an alias. You cannot change the option of an existing alias to Constant.

·          Private: The alias is available only within the scope specified by the Scope parameter. It is invisible in all other scopes.

·          AllScope: The alias is copied to any new scopes that are created.

 

To see the Options property of the aliases, type "get-alias | format-table -property Name, Definition, Options  -autosize".

 

The following lists the acceptable values for this parameter:

 

·          None

·          ReadOnly

·          Constant

·          Private

·          AllScope

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-passThru <SwitchParameter>

Passes the object created by this cmdlet through the pipeline. By default, this cmdlet does not pass any objects through the pipeline.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-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

Local

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-force <SwitchParameter>

Overrides restrictions that prevent the command from succeeding, just so the changes do not compromise security. For example, Force will override the read-only attribute or create directories to complete a file path, but it will not attempt to change file permissions.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-whatIf

Describes what would happen if you executed the command without actually executing the command.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-confirm

Prompts you for confirmation before executing the command.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

<CommonParameters>

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

 

INPUT TYPE

aliasObject

 

RETURN TYPE

There is no output unless the -passthru parameter is used.

 

NOTES

 

For more information, type "get-help set-alias -detailed". For technical information, type "get-help set-alias -full".

 

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

 

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

 

A cmdlet can have multiple aliases, but an alias can only be associated with one cmdlet at a time. If you use set-alias to associate the alias with a different cmdlet, it is no longer associated with the original cmdlet.

 

You can create an alias for a cmdlet, but you cannot create an alias for a command with parameters and values. For example, you can create an alias for Set-Location, but you cannot create an alias for "Set-Location C:\Windows\System32". 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. Profiles do not exist by default. To create a profile in the path stored in the $profile variable, type " new-item-type file -force $profile". To see the value of the $profile variable, type "$profile".

 

You can also save your aliases by using Export-Alias to copy the aliases from the session to a file, and then use Import-Aliasto add them to the alias list for a new session.

 

You can also refer to set-alias by its built-in alias, "sal". For more information, type "get-help About_Alias".

 

EXAMPLE 1

 

set-alias -name list -value get-childitem

 

This command creates the alias "list" for the Get-Childitemcmdlet. After you create the alias, you can use "list" in place of "Get-Childitem" at the command line and in scripts.

 

EXAMPLE 2

 

set-alias list get-location

 

This command associates the alias "list" with the Get-Locationcmdlet. If "list" is an alias for another cmdlet, this command changes its association so that it now is the alias only for Get-Location.

 

This command uses the same format as the command in the previous example, but it omits the optional parameter names, -Name and -Value. When you omit parameter names, the values of those parameters must appear in the specified order in the command. In this case, the value of -Name ("list") must be the first parameter and the value of -Value ("get-location") must be the second parameter.

 

EXAMPLE 3

 

set-alias scrub remove-item -option readonly -passthru | format-list

 

This command associates the alias "scrub" with the Remove-Itemcmdlet. It uses the "ReadOnly" option to prevent the alias from being deleted or assigned to another cmdlet.

 

The Passthru parameter directs Windows PowerShell to pass an object that represents the new alias through the pipeline to the Format-List cmdlet. If the Passthru parameter were omitted, there would be no output from this cmdlet to display (in a list or otherwise).

 

EXAMPLE 4

 

Set-Alias np c:\windows\notepad.exe

 

This command associates the alias, "np", with the executable file for Notepad. After the command completes, to open Notepad from the Windows PowerShell command line, just type "np".

 

This example demonstrates that you can create aliases for executable files and elements other than cmdlets.

 

To make the command more generic, you can use the "Windir" environment variable (${env:windir}) to represent the C:\Windows directory. The generic version of the command is "set-alias np ${env:windir}\notepad.exe".

 

EXAMPLE 5

 

function CD32 {set-location c:\windows\system32}

 

set-alias go cd32

 

These commands show how to assign an alias to a command with parameters, or even to a pipeline of many commands.

 

You can create an alias for a cmdlet, but you cannot create an alias for a command that consists of a cmdlet and its parameters. However, if you place the command in a function or a script, then you can create a useful function or script name and you can create one or more aliases for the function or script.

 

In this example, the user wants to create an alias for the command, " set-locationc:\windows\system32", where "set-location" is a cmdlet and "C:\Windows\System32" is the value of the Path parameter.

 

To do this, the first command creates a function called "CD32" that contains the Set-Location command.

 

The second command creates the alias, "go", for the CD32 function. Now, to run the Set-Location command, the user can type either "CD32" or "go".

 

RELATED LINKS

Get-Alias

New-Alias

Export-Alias

Import-Alias