Powershell getting started (10): Use the configuration file

Source: Internet
Author: User

Define aliases, variables, and functions in the command line, just add them to the current powershell session. After exiting the session, these changes will be lost. To use them in future sessions, you need to define them in the configuration file.

Configuration File

Powershell configuration files, unlike the. ini files in windowsProgramUse the XML file. Powershell configuration files are common powershell script files. Note that the file names of these configuration files are fixed and cannot be changed. And they are restricted by the script execution policy, that is, by default, when the script execution policy is restricted, these configuration files cannot be executed, that is, they are invalid.

Configuration file details

A total of four configuration files that affect powershell command lines are stored in the variable $ profile. allusersallhosts, $ profile. alluserscurrenthost, $ profile. currentuserallhosts and $ profile. currentusercurrenthost (can be abbreviated as $ profile. They apply to "all users and shells", "all users and the current shell", "all users and the current shell" respectively ". The preceding configuration files are given in the loading order. If there are conflicting items, the latter overwrites the former.

It should be noted that the concept of "user" is the same as that of a user in a computer. The "shell" or "Host Program" here refer to shell classification, instead of opening powershell examples (sessions ). There are two common shell types: powershell command line and powershell ise environment. There are other shell or host types that I have not carefully studied.

Powershell ise configuration file

Powershell ISE is also a common powershell environment. It also has four configuration files. Two configuration files that affect all hosts or shells are shared with powershell command lines. If the current host program or shell is affected, a special configuration file is available. The full names (including paths) of these configuration files are also stored in the $ profile attributes. The attribute names are the same as those in the powershell command line, but the attribute values are different, reflecting the configuration file name information of Ise.

Define the configuration file

Although the name of the configuration file has been determined, it is not automatically generated with the powershell installation, but must be created by the user as needed. Their creation process is the same as that of General scripts. For more information about script files, see "powershell getting started (8): functions, scripts, and scopes ". Make sure that the path and file name (including the extension. PS1) of the configuration file you created must be exactly the same as that defined by the system. Otherwise, the configuration file is invalid.

The four configuration files are defined in the same way. The following uses the configuration file that affects the current user's shell as an example to describe the process of defining the configuration file:

1. Create a configuration file

Any method for creating a text file is applicable here. Note that the file extension must be. ps1. For the sake of simplicity, we use the command class to create, the command is as follows:

 
New-item-itemtype file-Path$ Profile-Force

2. Edit the configuration file

Open the configuration file in notepad or any text editor and add your configuration items. You can define aliases, variables, and functions. You can also run commands to obtain the source and import modules by clicking. The following is an example of defining variables and getting the source by clicking. (Assume that I have a script file named myscript. ps1, which defines a function helloworld ())

$ Greet="Hello Luke!".. \ Myscript. PS1

Enter the preceding content into the configuration file, save and close the file.

3. Verify the configuration file

Open a new powershell command line, enter $ greet, and press Enter. The result is Hello Luke !; Enter helloworld and press Enter. Hellow, world! is displayed !. Open powershell Ise, enter $ greet, and press Enter. Enter helloworld to display the Command failure. Haha, verification passed.

Conclusion

The configuration file not only provides a simple solution for the unified powershell environment of the Company or project team, but also provides a configuration file with the same impact on all users' shells for all working machines, it also provides a way for programmers to customize the configuration file that affects the current user's shell. In addition, the configuration file improves the efficiency of powershell interactive command lines, but it may be a hidden risk for scripts that want to precisely control variable definitions and module imports. Therefore, consider carefully what content should be written into the configuration file.

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.