If you aren't familiar with PowerShell's personal profile scripts, they're simply plain text files containing commands that PowerShell executes when it starts up. These scripts don't exist unless you create them, so here's a quick walkthrough on how to set up a profile script and some information to help you understand how they fit into PowerShell's startup process.
How to Set Up a Profile Script
The first step in setting up a standard personal profile script is to find the pathname for that script. PowerShell keeps the pathname in the PowerShell variable $profile, even if the script doesn't exist. So, you can simply type
after the PowerShell prompt to see the literal path to that file. If you're running Windows XP from your C drive and are logged in as user Owner, $profile will be C:\Documents and Settings\Owner\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.
To create your profile script, open a text editor and type or paste in the code you want PowerShell to execute at startup, then save the file using the pathname shown in $profile. You might need to create the folder WindowsPowerShell before saving the file.
If the profile script fails to run and you receive the error message File <path to script file> cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details, you haven't enabled script execution on your system. You can check the current setting for the script execution policy by running the command
in the PowerShell window. You can also change the policy in the PowerShell window if you're running with administrative privileges. To tell PowerShell to always allow local scripts and trusted remote scripts to run, use the command
To tell PowerShell to run only scripts that have been signed with a trusted certificate, use
If the profile script still doesn't execute, make sure you aren't launching PowerShell from a shortcut that has the -NoProfile switch in the command line. PowerShell also won't run your profile script if you're running PowerShell as another user, such as Administrator.
How Profile Scripts Fit Into PowerShell's Startup Process
When Windows PowerShell starts up, it first runs an internal script, no matter what. This internal script is where settings such as the pre-existing PowerShell aliases and functions come from.
Next, PowerShell checks its own home folder for specific PowerShell scripts. This home folder is <System>\WindowsPowerShell\v<PowerShell-version-number>. If you're running the first release of PowerShell on a standard 32-bit Windows computer or 64-bit PowerShell on 64-bit Windows, this is typically just C:\Windows\System32\WindowsPowerShell\v1.0. If you're running 32-bit PowerShell on 64-bit Windows, however, the folder is C:\Windows\SysWOW64\WindowsPowerShell\v1.0. If there's any doubt, just start a PowerShell session as you normally would and after the PowerShell prompt enter
The path returned is the correct home path for that version of PowerShell.
No matter which home directory is being used, PowerShell runs two different scripts if they exist: first profile.ps1, then Microsoft.PowerShell_profile.ps1. There is a reason for two scripts. You can recompile PowerShell into custom hosts, and you might want to have certain commands run by all of these hosts. Profile.ps1 is a host-independent script that any host can run. Microsoft.PowerShell_profile.ps1 is the script that the standard PowerShell host runs.
After running these scripts, PowerShell then runs your personal profile script or scripts—just as in the system folder, PowerShell actually runs both profile.ps1 and Microsoft.PowerShell_profile.ps1 if they exist.
In general, it makes the most sense to use the personal Microsoft.PowerShell_profile.ps1 file in your Documents folder for most customizations. Using this file offers several advantages:
- The file is fairly easy to find once you've created it.
- Any changes you make won't affect any other PowerShell hosts on the system.
- The commands won't run automatically when you open a session with administrative privileges.
- The script will follow you around if you have networked profiles.
If you want to learn more about profile scripts, you can start by looking at the GettingStarted.rtf file that ships with PowerShell. It's located in a subfolder of $pshome\Documents. Other useful resources are the MSDN article "Windows PowerShell Profiles" (http://msdn2.microsoft.com/en-us/library/bb613488.aspx) and the TechNet Scripting Center article "Customizing the Windows PowerShell Console" (http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/console.mspx).