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
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
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).