Explore the use of Aliases_powershell under PowerShell (iii) PowerShell

Source: Internet
Author: User
Tags aliases

I do not know if you have contact with Linux, I talents, the knowledge of the alias is from Linux. Linux commonly used bash commands are hundreds, although in general the commands are very short, but often require a large number of parameters, so that for many beginners, the back command becomes the most headaches, but also makes many Linux beginners daunting. So, in Linux, the most common is alias, alias. Of course, PowerShell a good reference to this point.

This section is to introduce the PowerShell under the aliases.

In PowerShell, aliases can be used instead of commands in order to be used. For example, Windows veterans can take advantage of such as Dir, move, type, CLS, and so on, of course, in order to absorb more users, PowerShell also provides Linux alias settings, such as: LS, pwd, MV, man, cat and so on.

The purpose of setting aliases for PowerShell is to provide new users with a shell that can interact quickly. The alias here refers to the alternate name of the cmdlet. For example, "dir" replaces "Get-childitem".

In this section, you will describe two types of alias:

Built in aliases

Alternate name for the Windows,unix and predefined PowerShell cmdlet.

Use the following command to browse the PowerShell alias list:

Get-alias <enter>

Careful child shoes can be found, Get-chileitem this cmdlet has multiple alias, such as: dir, ls, GCI. We can try these commands and the results are the same as the following figure:

In essence, we only run four times "Get-chileitem". The presence of built-in aliases makes it easier and more efficient to work in a shell. However, to be honest, as a user, we do not need to pay more attention to a cmdlet have what the alias, we just use their habits on the line.

User-defined PowerShell alias

command to set Alias

Example: Defines an alias for a cmdlet named ' Get-service '

Set-alias GS Get-service <enter>

Quite a simple grammar, nothing more to say.

Using the above command to define the alias for your cmdlet is really easy, but there is a problem that the alias is only valid for the current session PowerShell, that is, when you close and reopen the PowerShell, enter the alias. PowerShell will be ruthless to tell you "the term ' is not recognized as a cmdlet, function, operable, or script file." Verify the term and try again.

So, what do we do when we create an alias and don't want to lose it?

• There are two options:

Import and export the PowerShell aliases;

Use the PowerShell configuration file to customize the alias.

Method One: Import and export the PowerShell aliases

One of the benefits of exporting aliases is that you can take it to any place you want to use it. Its commands are as follows < export filename customization:

Export-alias-path a.txt <enter>

We can use Notepad.exe to see what's inside of this stuff ~ ~ ~

Well, someone can't wait to try to import it ~ ~ ~

Import-alias-path a.txt <enter>

Well, the cups are!!!.

It looks miserable.

In fact, if a cmdlet has an alias, then importing again will produce such a cup.

So, our export file, we need to simply define their own commonly used alias (those systems are all removed from the all right), such as:

Well, that's it, hey heh, is not very evil ~ ~ See the effect of it:

Of course, powerful PowerShell are not retarded to allow users to manually load the aliases once each time the shell is started. In the PowerShell configuration file, we can make the relevant settings.

Method Two: Using the PowerShell configuration file

The location of the configuration file is stored in the $profile variable, and the default value is:

' My Documents\windowspowershell\microsoft.powershell_profile.ps1″

Use the following command to view the $profile value:

$Profile

As mentioned earlier, the PowerShell configuration file is a file with an extension of ". PS1". All PowerShell scripts use this extension. Here, I'll use Notepad to edit it. However, prior to this, you will need to check the execution policy settings for the PowerShell.

PowerShell the default execution policy for the first installation is "restricted", or "restricted", which means that PowerShell will not be able to run any scripts and configuration files.

PowerShell execution Policy rating:

Restricted-cannot run any scripts and configuration files

AllSigned-All scripts and configuration files must have a trusted publisher's signature

remotesigned-All scripts and configuration files can be downloaded from the Internet, but must have a trusted publisher's signature

Unrestricted-all scripts and configuration files will run, and scripts downloaded from the Internet will be prompted before they run.

Use the following cmdlet to view the policy level currently in place:

Get-executionpolicy <Enter>

To modify a policy level of unrestricted:

set-e Xecutionpolicy Unrestricted <enter>

OK, now we're going to configure the $profile.

• Step 1th to verify that $profile exists

Test-path $Profile <Enter>

False ===> configuration file does not exist (proceed to step 2).
True ===> don't say much, skip steps 2 and 3 directly. Unless you want to create a new configuration file, this will delete the current profile.

• Step 2nd to create a new configuration file

New-item-path $Profile-itemtype file-Force <enter> 

Step 3rd Verify that the new configuration file was successfully created

Repeat step 1. The result should be "true".

OK, open the configuration file in Notepad:

Notepad $Profile <enter>

Ok! We now have a blank configuration file that can actually be called a blank script.

Its file name is:

"Microsoft.powershell_profile.ps1″

Now let's start configuring:

OK, now it's time to close Notepad and PowerShell. Try opening PowerShell again.

Perfect!

Also need to explain a few points, when changing the PowerShell policy level, need to use administrator privileges, otherwise ...

All right, here's the section. To tell the truth, those strategies level words really bad remember, but fortunately not commonly used ~ ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.