Prof. Powershell

Profile Power: Session Configuration

Here's how to get what you want displayed every time you power up PowerShell.

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
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

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
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
PS C:\> $profile.AllUsersCurrentHost
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
PS C:\> $profile.CurrentUserAllHosts
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
PS C:\> $profile.CurrentUserCurrentHost
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PS C:\>

You'll see similar results in the PowerShell ISE.

[Click on image for larger view.] Figure 1.

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:

  1. All users all hosts
  2. All users current host
  3. Current user all hosts
  4. Current user current host

I tested with all profiles in the console:

[Click on image for larger view.] Figure 2.

And in the PowerShell ISE:

[Click on image for larger view.] Figure 3.

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.

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.

comments powered by Disqus
Most   Popular