Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0
When we open a PowerShell dialog box and create some variables (variables), functions (functions) inside, these variables and functions are only valid in the current session. Once we close this dialog and reopen PowerShell, these variables do not exist. If we want to keep these settings, we need to use profile, translation is the configuration file. The settings inside the configuration file are automatically imported when the PowerShell is started. This is a bit like autorun.bat, and if a friend with a DOS system still has an impression, it should know that.
The configuration files are stored in several places, different profiles, different scopes.
1,%WINDIR%\SYSTEM32\WINDOWSPOWERSHELL\V1.0\PROFILE.PS1
It works for all users, all shells.
2,%windir%\system32\windowspowershell\v1.0\ Microsoft.PowerShell_profile.ps1
works for all users, but only for Microsoft.PowerShell this shell. I don't know what that means, but is there a PowerShell shell that is not PowerShell? Uh, it's like tongue twisters.
3,%USERPROFILE%\My Documents\windowspowershell\profile.ps1
all shells that act on the current user.
4,%USERPROFILE%\My Documents\windowspowershell\microsoft.powershell_profile.ps1
Microsoft.PowerShell This shell that acts on the current user.
The PowerShell profiles of Windows above is not created automatically. The implication is that, if it is to be used, we will create it ourselves. We simply write our own content in accordance with the file path and file name given above.
There is a variable: $profile, which holds the path of the current profile. Use the Test-path $profile to see if there is currently a file. If not, you can use the New-item-path $profile-itemtype file-force command to create it. Then use the Notepad $profile to quickly open it to edit. We entered the function Pro {Notepad $profile}, hehe discerning all understand, later we want to modify profile, direct running Pro command on it.
Finally, if you want to PowerShell a successful load profile at startup, you will also need to set the PowerShell execution Policy (execute policy) to allow it to do so. Otherwise, attempting to load the configuration file will fail, and an error message will be displayed on the PowerShell interface. The error message for the configuration file could not be loaded as follows:
Copy Code code as follows:
C:\users\hong>powershell
Windows PowerShell
Copyright (C) is Microsoft Corporation. All rights reserved.
. : Unable to load file C:\Users\Hong\Documents\WindowsPowerShell\Microsoft.PowerShell
_PROFILE.PS1 because scripts are disabled on this system. For more information, see Http://go.micros
The about_execution_policies in the oft.com/fwlink/?linkid=135170.
Location: 1 Characters: 3
+ . ' C:\Users\Hong\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
+ Categoryinfo:securityerror: (:) [],pssecurityexception
+ fullyqualifiederrorid:unauthorizedaccess
In fact, solving this problem is the same as solving the PS1 file, because this profile is actually a PS1 format file. So you can use Set-executionpolicy remotesigned.
Reference article: Http://msdn.microsoft.com/en-us/library/bb613488%28VS.85%29.aspx