Profile Power: Session Configuration
Here's how to get what you want displayed every time you power up PowerShell.
- By Jeffery Hicks
If you've spent some time working in PowerShell, you've probably realized there are things you'd like in your session as soon as you start it. Items like variables, PSDrives, aliases or imported modules. This session configuration is done through a PowerShell profile script. This is a script, which means your execution policy applies. If you can't run scripts, you can't run a profile script.
The profile is hard-coded into PowerShell. You can see it like this:
PS C:\> $profile
The file does not exist by default. In fact, the WindowsPowerShell folder also does not exist by default. As far as I know you can't change this value. But you can create the file and stick in any commands and you have a profile script. Although, technically there are multiple potential profile scripts.
There are profile scripts that apply to you for the current PowerShell host or for all hosts. A host is the program running PowerShell like PowerShell.exe or the PowerShell ISE. You might have other products installed that can also use profile scripts. There are also profiles that apply to all users of a computer. Again, for the current host or for all hosts. All of these paths are hardcoded as well.
PS C:\> $profile.AllUsersAllHosts
PS C:\> $profile.AllUsersCurrentHost
PS C:\> $profile.CurrentUserAllHosts
PS C:\> $profile.CurrentUserCurrentHost
You'll see similar results in the PowerShell ISE.
You'll notice there are some duplicates and some variations in the file names. All of these files will potentially run if they exist. The net effect is cumulative depending on the host.
Of course you may be wondering what order the profiles are processed. I did to. The order is:
- All users all hosts
- All users current host
- Current user all hosts
- Current user current host
I tested with all profiles in the console:
And in the PowerShell ISE:
You probably won't need the all user profiles, unless you have settings you want to apply to a PowerShell session regardless of who logs on. But if you are like me, you spend time in the both the console and the PowerShell ISE. I use 3 profile scripts.
In the current user all hosts profile I put in commands that I want for both the console and ISE. Things like common variables, functions, imported modules and PSDrives. Then I use the host specific profile scripts. I have a few things I do that only make sense in the console and some things that only work in the PowerShell ISE. The net result is that no matter if I'm in the console of the ISE, I have a properly configured environment.
But don't feel you have to follow my lead. I've been using PowerShell long enough to have some specialized needs. I think for most people, if you use the profile for the current user and all hosts, the one called profile.ps1 in Documents\WindowsPowerShell you'll do just fine. So put some power in your profiles and make your PowerShell experience a productive one.
Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide.
You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)