Summary: Microsoft Scripting Guy, Ed Wilson, discusses the six different Windows PowerShell profiles, and when to use each.
Hey, Scripting Guy! Dude, I have been reading some of the posts in your most excellent blog; and first of all, I want to say I think you are great. Now for the question: I do not get the Windows PowerShell profile. I mean I get it, but not really. Here is part of my problem. I put some things in the profile, and then I go back and they are not there. Like, what is up with that? I hope you can help me. By the way, I am, like, totally looking forward to seeing you and the Scripting Wife at TechEd 2012 in Orlando. You will know me, because I sort of look like Urkel, and I always wear plaid shirts (but I don’t wear suspenders).
Microsoft Scripting Guy, Ed Wilson, is here. Last week was an absolutely great week. The Scripting Wife and I had dinner one night with Rich from the NYC Windows PowerShell Users Group (he is also a moderator for the Scripting Guys forum and writer of a couple of guest blogs). Rich was kind enough to bring me some Gunpower green tea, and I am sipping some right now. It is wonderful with a half teaspoon of organic lavender added to the pot. We also had the PowerScripting Podcast with the two winners of the 2012 Scripting Games and Jeffrey Snover. That conversation was fun and informative. I also enjoy talking to Jeffrey, and I look forward to sitting-in on at least one of his sessions at Microsoft TechEd 2012.
Six, count ‘em, six different PowerShell profiles
BB, there is no doubt that you are a bit confused with Windows PowerShell profiles. There are, in fact, six different profiles. The Windows PowerShell console and the Windows PowerShell ISE have their own profiles. In addition, there are profiles for the current user and profiles for all users. The table that follows lists the six profiles and their associated locations.
The first thing to do to understand the six Windows PowerShell profiles is to keep in mind that they move. They change (sort of like the staircases at Hogwarts). As long as you realize that they are a moving target, you will be fine. In most cases, when talking about the Windows PowerShell profile, people are referring to thecurrent user, currenthost profile. In fact, if no one qualifies the Windows PowerShell profile with its associated scope or description, it is safe to assume that they are talking about the Current User, Current Hostprofile.
Note A Windows PowerShell profile (any one of the six) is simply a Windows PowerShell script. It has a special name, and it resides in a special place, but it is simply a script. In this regard, it is sort of like the old-fashioned autoexec.bat batch file. Because the Windows PowerShell profile is a Windows PowerShell script, you must enable the Script Execution policy prior to configuring and using a Windows PowerShell profile. For information about the Script Execution policy refer to this collection of Hey, Scripting Guy! Blogs.
Examining the $profile variable
When you query the $profile automatic variable, it returns the path to the Current User, Current Hostprofile. This makes sense, and it is a great way to easily access the path to the profile. The following script illustrates this technique from within the Windows PowerShell console.
The difference between the Windows PowerShell console Current User, Current Hostprofile path and the Windows PowerShell ISE Current User, Current Host profile path is three letters: ISE.
BB, these three letters are probably causing you problems. More than likely, you are setting something in your Windows PowerShell console profile, and it is not available inside the Windows PowerShell ISE.
Unraveling the profiles
You can pipe the $profile variable to the Get-Member cmdlet and see additional properties that exist on the$profile variable. This technique is shown here.
PS C:\> $PROFILE | Get-Member -MemberType noteproperty | select name
If you are accessing the $profile variable from within the Windows PowerShell console, theAllUsersCurrentHost and the CurrentUserCurrentHost note properties refer to the Windows PowerShell console. If you access the $profile variable from within the Windows PowerShell ISE, theAllUsersCurrentHost and the CurrentUserCurrentHost note properties refer to the Windows PowerShell ISE profiles.
Using the $profile variable to refer to more than the current host
When you reference the $profile variable, by default it refers to the Current User, Current Hostprofile. If you pipe the variable to the Format-List cmdlet, it still refers to the Current User, Current Host profile. This technique is shown here.
This leads to a bit of confusion, especially because the Get-Member cmdlet reveals the existence of multiple profiles and multiple note properties. The way to see all of the profiles for the current host, is to use the forceparameter. It reveals the hidden properties. The command illustrating this technique is shown here.
$PROFILE | Format-List * -Force
The command and the associated output from the command are shown in the image that follows.
It is possible to directly access each of these specific properties just like you would access any other property—via dotted notation. This technique is shown here.
The path to each of the four profiles for the Windows PowerShell console are shown in the image that follows.
Determine if a specific profile exists
To determine if a specific profile exists, use the Test-Path cmdlet and the appropriate flavorof the $profilevariable. For example, to determine if a Current User, Current Hostprofile exists you can use the $profilevariable with no modifier, or you can use the CurrentUserCurrentHost note property. The following example illustrates both of these.
PS C:\> test-path $PROFILE
PS C:\> test-path $PROFILE.CurrentUserCurrentHost
In the same manner, the other three profiles that apply to the current host (in this example, I am using the Windows PowerShell console) are determined to not exist. This is shown in the code that follows.
PS C:\> test-path $PROFILE.AllUsersAllHosts
PS C:\> test-path $PROFILE.AllUsersCurrentHost
PS C:\> test-path $PROFILE.CurrentUserAllHosts
Creating a new profile
To create a new profile for current user all hosts, use the CurrentUserAllHostsproperty of the $profileautomatic variable, and the New-Item cmdlet. This technique is shown here.
To open the profile for editing, use the isealias as shown here.
When you are finished editing the profile, save it, close the Windows PowerShell console, reopen the Windows PowerShell console, and test that your changes work properly.
BB, that is all there is to using the $profile variable to discover different Windows PowerShell profiles. Windows PowerShell Profile Week will continue tomorrow when I will talk about editing and testing a Windows PowerShell profile.
Nice explanation. You could possibly also create a static link from one to the other, althoguh i know i have different things in my console profile than my ise profile.
22 May 2012 12:53 PM
@Jeffrey S. Patton yes, you are correct about possibly linking one to the other in the profiles. In the article for the 23rd, I talk about using a central file for your profile. One way to get around problems of incompatable commands is to detect the host, and use an if statement. Of course, my preference is to actually use a module (or several modules) for a profile.