Explore how to use Aliases in PowerShell (3)

Source: Internet
Author: User

I don't know if you have ever been familiar with linux. I have never been familiar with alias since linux. There are also hundreds of Bash commands commonly used in Linux. Although the commands are generally very short, many parameters are required in many cases. In this way, for many beginners, back-to-Back commands have become the biggest headache for many Linux beginners. Therefore, in linux, alias is a common alias. Of course, PowerShell has a good reference for this.

This section describes Aliases under PowerShell.

In PowerShell, aliases can be used to replace commands. For example, Windows Veterans can use tools such as dir, move, type, and cls. Of course, PowerShell also provides Linux alias settings to attract more users, such: ls, pwd, mv, man, cat, etc.

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

This section describes two types of alias:

Built-in alias

The alternative name for Windows, Unix, and predefined PowerShell cmdlet.

Run the following command to view the alias list of PowerShell:

get-alias <enter>

Careful kids shoes can find that the Get-ChileItem cmdlet has multiple alias, such as dir, ls, and gci. We can try these commands and the results are the same, for example:

Essentially, we only run "get-chileitem" four times ". The existence of built-in aliases makes it easier and more efficient for us to work in shell. But to be honest, as a user, we don't need to pay much attention to the alias of A cmdlet. We just need to make good use of what we get used.

Alias

Set alias commands

For example, define an alias

Set-Alias gs Get-Service <enter>

There is nothing to say about the simple syntax.

Using the above command to define alias for your cmdlet is indeed very easy, but there is a problem, that is, this alias is only valid for the PowerShell currently in the session period, that is, when you close and re-open PowerShell, input this alias, PowerShell will relentlessly tell you that "The term 'gs 'is not recognized as a cmdlet, function, operable program, or script file. verify the term and try again."

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

• There are two options:

Import and Export PowerShell Aliases;

Use the PowerShell configuration file to customize Alias.

Method 1: Import and Export PowerShell Aliases

One advantage of exporting Aliases is that you can take it to any place you want to use. The command is as follows <custom Export File Name>:

Export-Alias -Path a.txt <enter>

We can use notepad.exe to see if there is anything in it ~~~

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

Import-Alias -Path a.txt <enter>

Amount ~ Cup !!!

Looks miserable ......

In fact, if a cmdlet has alias, the next import will produce such a cup.

Therefore, to export a file, you just need to define the commonly used alias (all the built-in systems can be deleted), such:

Okay, that's it. Hey, is it evil ~~~ Let's see the results:

Of course, powerful PowerShell won't be mentally retarded so that users can manually load aliases once each time they start the shell. In the PowerShell configuration file, we can perform relevant settings.

Method 2: Use the PowerShell configuration file

The location of the configuration file is stored in the $ Profile variable. The default value is:

'My Documents \ WindowsPowerShell \ Microsoft. PowerShell_profile.ps1 ″

Run the following command to view the $ Profile Value:

$Profile

As mentioned above, the PowerShell configuration file is a file with the extension ". ps1. All PowerShell scripts use this extension. Here, I will use NotePad to edit it. However, before that, you also need to check the PowerShell execution policy settings.

When PowerShell is installed for the first time, the default execution policy is "Restricted", that is, "Restricted", which means PowerShell will not be able to run any scripts and configuration files.

• Classification of PowerShell execution policies:

Restricted-you cannot run any scripts or configuration files.

AllSigned-all scripts and configuration files must have the signatures of trusted publishers.

RemoteSigned-all scripts and configuration files can be downloaded from the Internet, but must have the signature of a trusted publisher.

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

Use the following cmdlet to view the current policy level:

Get-ExecutionPolicy <Enter>

Modify the policy level to Unrestricted:

Set-ExecutionPolicy Unrestricted <enter>

Okay. Now we will configure $ Profile.

• Step 4: Verify that $ Profile exists

test-path $Profile <Enter>

False ==> the configuration file does not exist (continue step 2 ).
True ==> skip steps 2 and 3. Unless you want to create a new configuration file, this will delete the current configuration file.

• Create a new configuration file in step 2

New-Item -Path $Profile -ItemType file -Force <enter> 

Step 2: verify whether a new configuration file is successfully created

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

Okay. Open the configuration file in Notepad:

notepad $Profile <enter>

OK! We now have a blank configuration file, which can be called a blank script.

The file name is:

"Microsoft. PowerShell_profile.ps1 ″

Configure it now:

Now you can disable notepad and Powershell. Open PowerShell again.

Perfect!

In addition, you must specify the following points. You must use the Administrator permission to change the PowerShell policy level. Otherwise ......

Well, this section is here. To be honest, the words at the policy level are really hard to remember, but fortunately they are 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.