Prof. Powershell
Profile Pro
Get a handle on PowerShell's profile scripts, used for configuring your PS sessions.
- By Jeffery Hicks
- 04/26/2010
Windows PowerShell uses a special script, often referred to as a profile, to configure your PowerShell session. If you have particular custom functions, aliases or variables that you always use, put the command in your profile script and they will become part of your session when you start PowerShell.
There are a number of possible profile scripts, none of which exist by default. You can run this expression in a standard PowerShell console to see them:
PS C:\> $profile | select *Host* | format-list
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost
: C:\Windows\System32\WindowsPowerShell\v1.0\
Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts
: C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost
: C:\Users\Jeff\Documents\WindowsPowerShell\
Microsoft.PowerShell_profile.ps1
Okay, so this might be old news. So, this time I want to focus on the profile scripts for the current user. I've been transitioning to a new computer and also find myself spending more time in the PowerShell Integrated Scripting Environment (ISE). However, anything I define in my standard profile, $profile, doesn't get applied when in the ISE. That's because the ISE has its own profile:
PS C:\Scripts> $profile
C:\Users\Jeff\Documents\WindowsPowerShell\
Microsoft.PowerShellISE_profile.ps1
Because most of my profile commands will work fine in both the standard shell and the ISE, I realized that rather than maintaining two copies of essentially the same profile, that I should use the profile for the current user that applies to all hosts: C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1.
In this profile I put in all the commands I want to configure both shells, which is most of my original standard shell. Then I put in entries for the shell specific profiles that will only work in those shells. I have a function that only works with the ISE plus I want to import an ISE-specific module so my ISE specific profile looks like this:
#dot source some ISE specific functions
. c:\scripts\load-script.ps1
#configure the ISE
$psise.options.fontsize=16
$psise.options.fontname="consolas"
#import an ISE specific module
Import-Module ISEPack
My standard PowerShell profile likewise is now limited to console-specific settings:
#reconfigure the console
[console]::Title="Windows PowerShell 2.0"
[console]::BackgroundColor="black"
[console]::ForegroundColor="green"
The CurrentUserAllHosts profile runs first then the host specific profile. Yes, I now have three profile scripts to manage, but unless I'm adding a configuration that is host-specific, all I really need to worry about is the profile script for CurrentUserAllHosts.
So if you find yourself using more than one PowerShell host, take a look at your profile scripts and see what needs to be done to make them efficient and turn yourself into a PowerShell Pro.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.