Aliases


As intuitive as PowerShell's cmdlet names can be, they're not always convenient to type. For example, typing Set-Location is a poor substitute for the good ol' cd command under Cmd.exe. That's why PowerShell lets you define aliases, or nicknames, for cmdlets. For example, if you find Pop-Location to be too cumbersome, you can create a nickname called "Popl" for it (an alias called "popd" is already defined by PowerShell):

 PS C:\>Set-Alias popl Pop-Location 

Now you can use Popl in place of Pop-Location. You can remove, or undefine, an alias by using the generic Remove-Item cmdlet:

 PS C:\>Remove-Item alias:popl 

This removes the Popl alias from the system. PowerShell predefines a number of useful aliases. You can see a list of these aliases by running Get-Alias. Here's a portion of the output you'll see:

 CommandType     Name                           Definition -----------     ----                           ---------- Alias           ac                             add-content Alias           clc                            clear-content Alias           cli                            clear-item Alias           clp                            clear-property Alias           clv                            clear-variable Alias           cpi                            copy-item Alias           cpp                            copy-property Alias           cvpa                           convert-path Alias           epal                           export-alias Alias           epcsv                          export-csv Alias           gci                            get-childitem 

Note that you can only set up aliases for cmdlets; aliases aren't shortcuts for entire command strings. For example, the following won't work:

 PS C:\>Set-Alias GoC "Set-Location C:\" 

Aliases can only be for a single cmdlet or external executable, not for any accompanying parameters or arguments. However, you can write a script that performs more complex tasks such as executing cmdlets that have arguments, and create an alias to the script.

When you define an alias, it only lasts for the current PowerShell session. To make an alias permanent, add it in your profile, which is a special PowerShell script that runs when you start the shell. We'll talk more about profiles in a bit.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net