Cmdlet names can be cumbersome to type. To minimize typing, and to make it easier for users accustomed to other shells to use Windows PowerShell, Windows PowerShell supports the concept of an alias, that is, an alternate name for a command. You can create an alias for a cmdlet name, function name, or the name of an executable file, and then type the alias instead of the name in any command.

Windows PowerShell includes many built-in aliases and you can create your own. The aliases that you create are valid only within the current session. To create a persistent alias, add the alias to your Windows PowerShell profile.

To find all of the aliases in your session, type:

get-alias

To find the aliases for a cmdlet, type:

get-alias | where-object {$_.definition -eq "<cmdlet-name>"}

For example:

get-alias | where-object {$_.definition -eq "set-location"}

The aliases in Windows PowerShell are supported by the Windows PowerShell Alias provider, a .NET Framework assembly that lets you view the aliases in a drive that looks much like the file system drives in Windows. The drive for aliases is Alias:.

To change to the Alias drive, type:

set-location alias:

To view the aliases, that is, the child items in the Alias: drive, type

get-childitem

To view the child items in the Alias: drive from another drive, include the drive name in the command. For example:

get-childitem alias: 

Creating an Alias

To create aliases for cmdlets and commands in Windows PowerShell, use the Set-Alias cmdlet. For example, to create the "gh" alias for the Get-Help cmdlet, type:

set-alias gh get-help

You can also create aliases for commands, such as the commands that start a program. For example, to create the alias "np" for Notepad, type:

set-alias np c:\windows\notepad.exe

(The path to Notepad might be different on your system.)

Deleting an Alias

To delete an alias, use the Remove-Item cmdlet to delete the alias from the Alias: drive. For example, to remove the "ls" alias, type

remove-item alias:ls

Using Functions to Create Alternate Names

You can create an alias for a cmdlet, function, or executable file, but you cannot create an alias for a command with parameters. You can, however, create a function that behaves much like an alias.

For example, to use Notepad to open the Boot.ini file on a computer running Windows XP, type:

notepad c:\boot.ini

You cannot create an alias for "notepad c:\boot.ini", but you can create a function. The following command creates the bootini function.

function bootini {notepad c:\boot.ini}

This function behaves like an alias. If you type bootini at the Windows PowerShell prompt, Boot.ini opens in Notepad.




Table Of Contents