Set-Alias

Create or change an alias. An alias is an alternate (usually shorter) name for a cmdlet, script, function, or executable file.

Syntax
      Set-Alias [-name] string [-value] string [-description string] 
         [-option Option] [-passThru] [-scope string] 
            [-force] [-whatIf] [-confirm] [CommonParameters]
Key
   -name string[]
       The alias to create, the first character cannot be a number.
 
   -value string
       The name of the cmdlet or command element that is being aliased.

   -description string
       A description for the alias.

   -option option
       The valid options are: 
         None    : Set no options. (default) 
         ReadOnly: The alias cannot be changed unless you use -Force. 
         Constant: The alias cannot be changed, even by using -Force. 
         Private : The alias is available only within the scope specified by -Scope. 
                   It is invisible in all other scopes.
         AllScope: The alias is copied to any new scopes that are created.

   -passThru 
       Return an object representing the alias.
       By default, this cmdlet does not generate any output. 

   -scope string
       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, type "get-help about_scope".

   -force
       Allows the cmdlet to set a read-only alias.
       Use -Option to create a read-only alias.
       -Force cannot be used to edit a constant alias.
        
   -whatIf
       Describe what would happen if you executed the command without actually
       executing the command.
       
   -confirm
       Prompt for confirmation before executing the command.

Standard Aliases for Set-Alias: sal

Changes you make to an alias are lost when you exit the session or close PowerShell unless you add the alias to the startup PowerShell profile. You can also use Export-Alias and Import-Alias to save and restore alias information from a file.

Aliases are primarily designed to promote high-speed interactive use of PowerShell, when writing scripts that will be shared with other users it is often better to use the full cmdlet names as this will improve the scripts readability.

You can assign an alias to a cmdlet, script, function, or executable file, but you cannot assign an alias to a command and its parameters. For example, you can assign an alias to Get-Eventlog, but you cannot assign an alias to Get-Eventlog -logname security. However, you can create a function that includes such a command.

The 'built-in' aliases supplied with PowerShell have the option AllScope set, so they will survive any changes in scope.
The 'built-in' aliases that are idiomatic to PowerShell also have the ReadOnly option set.
Removing or redefining the built-in read-only Aliases is possible (using -FORCE) but is strongly discouraged.

Read-Write aliases such as cat or ls may be redefined to mimic the functionality of other shells. For example, you might create a function that acts as an improved get-content, call it get-content2 and then redefine cat as get-content2, this leaves the original cmdlet name available unchanged for use in scripts that you share with other users.

Token resolution works by first expanding aliases then binding to a function, then a cmdlet, lastly as an external executable.

Examples

Create an alias named 'list' to represent Get-ChildItem:

PS C:\> set-alias list get-childitem

Use Get-Alias to display information about the new alias called 'List':

PS C:\> get-alias -name list | format-list
DisplayName : list -> Get-ChildItem
CommandType : Alias
Definition : get-childitem
ReferencedCommand : Get-ChildItem
ResolvedCommand : Get-ChildItem

Delete an alias named 'list'

PS C:\> Remove-item alias:list

Create a ReadOnly alias named 'ci' to represent Get-CIMInstance:

PS C:\> set-alias -name ci -value get-CIMinstance -description "CIM alias" -option ReadOnly

Associate the alias, 'np', with the executable file for Notepad:

PS C:\> Set-Alias np c:\windows\notepad.exe

Create a function that will set location as C:\windows\system32 and then assign the alias "cd32", to the new function:

PS C:\> function func32 {set-location c:\windows\system32}
PS C:\> set-alias cd32 func32 

"When people realize that they do not understand a thing they try to find a name for what they do not 'understand', and when they find a name they say they 'understand' " ~ G.I. Gurdjieff

Related PowerShell Cmdlets

Export-alias - Export an alias list to a file.
import-alias - Import an alias list from a file.
Get-alias - Return alias names for Cmdlets.
New-alias - Create a new Cmdlet-alias pairing.
Equivalent bash command: alias - Create an alias.


 
Copyright © 1999-2024 SS64.com
Some rights reserved