You define aliases, variables, and functions on the command line, just adding them to the current PowerShell session. When you exit the session, these changes are lost. To be able to use them in future sessions, you should define them in a configuration file.
Configuration file
The PowerShell configuration file does not use the. ini file as it does in the Windows operating system, nor does it use the XML file as most applications do. PowerShell's configuration files are common PowerShell script files. It should be noted that the file names of these profiles are fixed and cannot be changed. And they are constrained by the script execution policy, which means that, by default, the script execution policy is restricted, and that the configuration files cannot be executed, which is not valid.
Configuration file Details
A total of 4 configuration files that affect the PowerShell command line are stored in the variable $profile respectively. Allusersallhosts, $PROFILE. Alluserscurrenthost, $PROFILE. Currentuserallhosts and $profile. Currentusercurrenthost (can be abbreviated to $profile). They are scoped to all user shells, all user current shells, current user all shells, and current user shell. The above configuration file is given in the order of loading, and if there are conflicting items, the latter overrides the former.
It should be explained that the concept of "user" is the same as the user concept in the computer; here the "shell" or "host program" refers to the shell taxonomy rather than the Open PowerShell sample (session). There are two common types of shell, PowerShell command line and PowerShell ISE environment. There are other shell or host types, I am not careful.
PowerShell ise configuration file
PowerShell Ise is also our common PowerShell environment, and it also has 4 configuration files. Two of the configuration files that affect all hosts or shells are shared with the PowerShell command line, which affects the current host program or shell, with a specific profile. The full name of these profiles (including the path) is also stored in the $profile property, which is consistent with the PowerShell command line, but with a different property value that reflects the ISE profile name information.
To define a configuration file
Configuration files, although their names are determined, are not automatically generated with the PowerShell installation, but require users to create them themselves when they are needed. They are created in the same way that a generic script is created. For more information about script files, see "PowerShell Getting Started (eight): Functions, scripts, scopes." However, it is important to ensure that the path and file name (including extension. PS1) of the configuration file you create are exactly the same as the system definition, otherwise it is not valid.
The four configuration files are defined in exactly the same way, and the following is an example of a configuration file that affects the current user's shell, describing the process of defining a profile:
1. Create configuration file
Any way to create a text file is applicable here. Note, however, that the file extension must be. ps1. For simplicity, we use the command class to create the following commands:
Copy Code code as follows:
New-item-itemtype File-path $PROFILE-force
2, edit the configuration file
Open the configuration file with Notepad or any text editor to add your configuration items. These configuration items can actually define aliases, variables, functions, or you can run commands with a dot "." Get sources, import modules, and so on. Here is the definition of the variable and the point "." Gets an example of a source. (Suppose I have a script file named Myscript.ps1, which defines a function HelloWorld ())
Copy Code code as follows:
$greet = "Hello luke!"
. . \myscript.ps1
Enter the above content into the configuration file, save, close.
3, verify the configuration file
Open a new PowerShell command line, enter the $greet, return, the results show Hello luke!; enter HelloWorld, return, and the results show Hellow, world!. Open PowerShell ISE, enter $greet, return, nothing; enter HelloWorld to display an error that failed to find the command. Haha, verify through.
Conclusion
The configuration file provides a simple solution for a company or project team to unify the PowerShell environment-providing all of the machines with the same configuration files that affect all users ' shells, as well as providing a way for the programmer's personalization requirements-to define the profile that affects the current shell of the current user. In addition, it is obvious that configuration files improve the efficiency of PowerShell interactive command lines, but they can be a potential concern for scripts that want to precisely control variable definitions and module imports. Therefore, what content should be written to the configuration file, please consider carefully.