All those PowerShell profiles

I have been playing with PowerShell a lot since V2 CTP3 was posted and will write about setting up custom menus in the PowerShell Integrated Scripting Environment (PowerShell ISE). Before I get to that, though, I wanted to say something about the profiles.

In particular: $profile and what you get back from its default ToString() method.

On my computer, $profile gives me:

"C:\Users\Tommy\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"

I had always treated that as the base profile. But it’s not. Although the $profile variable is a string, there are four NoteProperties attached (use $profile | get-member to see them): AllUsersAllHosts, AllUsersCurrentHost, CurrentUserAllHosts, and CurrentUserCurrentHost. Turns out that the default value of $profile is CurrentUserCurrentHost.

AllUsersAllHosts $pshome\profile.ps1
AllUsersCurrentHost $pshome\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts [Environment]::GetFolderPath("MyDocuments")\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost [Environment]::GetFolderPath("MyDocuments")\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Up to this point, I had been thinking of $profile as though it were CurrentUserAllHosts and didn’t understand why my aliases and functions didn’t work in PowerShell ISE.

Once I moved things to profile.ps1 from Microsoft.PowerShell_profile.ps1, I was happier. And, of course, all those customizations I want to make to PowerShell ISE go in Microsoft.PowerShellISE_profile.ps1.

I’m still not sure I agree that the default value of $profile should be CurrentUserCurrentHost but I haven’t heard many complaints about this so maybe I’m wrong. But I have always used ". $profile" to reload my profile and that doesn’t work. Now I need the more cumbersome ". $profile.CurrentUserAllHosts" followed by ". $profile"–and this assumes I don’t have anything in the AllUsers profiles.

Which of the four NoteProperties do you think $profile should return? Leave a comment and let me know.

2 Comments

  1. Jeff Hicks says:

    Right now you only really have one PowerShell host, powershell.exe. The ISE in CTP3 is a exception that I’m going to ignore for now because it might change. Given that, I would expect $profile to return my profile for the current host. At some point we might have several PowerShell hosts running on our desktops at which point the other profiles will become more relevant. It’s also possible that these yet to be developed hosts might have their own profiles, and you very well might want to keep them separate. I guess the bottom line is, read the documentation for whatever PowerShell host or build you are running and plan your profiles accordingly.

    Jeffery Hicks
    Microsoft PowerShell MVP
    http://blog.sapien.com
    follow me: http://www.twitter.com/JeffHicks

    “Those who forget to script are doomed to repeat their work.”

    Now Available: Managing Active Directory With Windows PowerShell: TFM

  2. Interesting point about the PowerShell ISE host possibly changing: but I do think you need separate profiles for PowerShell and PowerShell ISE, if only due to the customizations available like custom menus and commands, and even colors.

Leave a Reply