Learn about Windows PowerShell
Hey, Scripting Guy! I would like to personalize the way that Windows PowerShell works. I have been hearing that I can use a thing called a profile to do this, but when I try to find information about profiles, I come up blank. There is no New-Profile Windows PowerShell cmdlet so I do not see how to create such a thing. Can you help me please?
Microsoft Scripting Guy Ed Wilson here. It is another beautiful day in Charlotte, North Carolina—at least it would be. I had an early morning visit to the doctor this morning, and you know how that can go. Interestingly enough, one of the nurses commented to the Scripting Wife that I was not a morning person because I was always grumpy when she saw me. Not a morning person? I get up early every day of the week. Grumpy? Me? I am one of the most laid back people you could ever meet. Then it dawned on me. The only time she sees me, she is giving me a shot or draining a couple quarts of blood from my veins. That must be why she thinks I am grumpy.
AC, it is interesting you talk about changing the way Windows PowerShell behaves because Windows PowerShell is highly configurable. The way you see it today may not be the way you see it tomorrow. If you were to use Windows PowerShell on my computer, it might behave differently. The environment controls it all—and the Windows PowerShell profile is a major part of that environment.
Note: Portions of today's Hey, Scripting Guy! article are excerpted from the Microsoft Press book, Windows PowerShell 2.0 Best Practices by Ed Wilson. This book is available for pre-order.
When Windows PowerShell is first installed, there are no profiles installed on the computer. In one respect you can consider the profile to be like the autoexec.bat file was a few years ago. The autoexec.bat file was simply a batch file that executed batch commands. However, because it was located in the root and had the name autoexec.bat, it took on an importance that was greatly out of proportion to a simple batch file because the commands that existed in the file were used to configure all kinds of activities including configuring the environment and even launching Windows itself. The Windows PowerShell profile does not launch Windows PowerShell. Instead, it is a Windows PowerShell script that happens to have a special name and happens to exist in a special place, or rather it happens to have two special names and exists in four special places. You heard me correctly. There are actually four Windows PowerShell profiles. These four profiles are listed in Table 1.
Table 1 Windows PowerShell Profiles and Locations
In addition to the four Windows PowerShell profiles, other applications that host Windows PowerShell can create their own profiles. For example, the Windows PowerShell Integrated Scripting Environment (ISE) hosts Windows PowerShell and has two additional profiles. These two profiles are listed in Table 2.
Table 2 Windows PowerShell ISE Profiles and Locations
Two of the profiles are used by all Windows PowerShell users on a computer. Anything placed in the All Users profile will be available to any script or to any user that runs Windows PowerShell on the computer. As a result, you should be rather circumspect in what you place in the All Users profile. However, these are great locations to configure aliases that you wish to make available to all users, variables you intend to use in a corporate scripting environment, or a Windows PowerShell drive or a function.
The next question is which one of the two All Users profiles should you use? The AllUsersAllHosts profile will apply to all of the users on the computer. It also applies to every instance of Windows PowerShell that may run on the computer. This includes the Windows PowerShell console, the Windows PowerShell ISE, and anything else that may host Windows PowerShell. This would include the Exchange Management environment, the SQL console, and any application that could host Windows PowerShell. Even if you are careful with the aliases you create, the variables you assign, the functions you write, and any Windows PowerShell drives you decide to make, you will still need to test to ensure compatibility. The AllUsersCurrentHost profile would give you the same ability to modify the Windows PowerShell environment for all users, but would only apply to the console host.
The two current user profiles are used to modify the Windows PowerShell environment for the current user. The profile that is most often modified by a user to configure personal Windows PowerShell settings is the CurrentUserCurrentHost profile. This one is referenced by the $profile automatic variable. On my computer, the value of the $profile variable is shown here:
PS C:\> $PROFILEC:\Users\edwilson\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
You can see that, on a Windows Vista computer (or on a Windows 7 computer), the user's personal folder is in the user’s Documents folder. The WindowsPowerShell folder does not exist if no profile has been created. This is seen here, where the Test-Path cmdlet is used to see if the parent folder that should contain the Microsoft.Powershell_profile.ps1 file exists. Because no personal profile has yet been created on this laptop, the WindowsPowerShell folder has not been created:
PS C:\> Test-path (Split-Path $PROFILE -Parent)False
Keep in mind that some applications will create an application-specific profile and store it in the WindowsPowerShell folder. On one of my computers, I do not have a Windows PowerShell profile configured. I do, however, have Sapien’s Primal Script installed, and Primal Script creates an application-specific profile. The test for the parent folder, therefore, will pass as shown here:
PS C:\> Test-Path (Split-Path $PROFILE -Parent)
PS C:\> Test-Path $PROFILE
To explore what is contained in the $PROFILE –Parent folder, you can call Windows Explorer from the command line by using the Invoke-Item cmdlet. This is seen here:
PS C:\> Invoke-Item -Path (Split-Path $PROFILE -Parent)
You can also call Windows Explorer directly by using the executable name as seen here:
PS C:\> explorer (Split-Path $PROFILE -parent)
Calling Windows Explorer directly does not really buy you anything, particularly because you can use aliases to shorten the typing. This is seen here:
PS C:\> ii (Split-Path $PROFILE -Par)
However you get there, the Windows Explorer directory is set to the WindowsPowerShell directory. This is seen here:
To create a CurrentUserCurrentHost profile, use the New-Item cmdlet as shown in the following code. When using the New-Item cmdlet, specify the –force if the folder does not exist, and specify the ItemType as file. This is shown here:
New-Item -Path $PROFILE -ItemType file –Force
After you create the profile, you can open it in Notepad or in the Windows PowerShell ISE to edit the file. If you choose to edit it in Notepad, it is as simple as typing Notepad and giving it the automatic $profile variable as shown here:
The newly created Windows PowerShell profile opens in Notepad and is ready for editing. This is seen here:
Of course, AC, there is nothing in the Windows PowerShell profile at this stage of the game. We will add to our profile tomorrow as Windows PowerShell Profile Week continues.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at email@example.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, keep on scripting!
Ed Wilson and Craig Liebendorfer, Scripting Guys