Like the standard PowerShell window, the SharePoint Management Shell, can be customized to fit your personal style. To see this in action, open up the SharePoint Management Shell and enter the following line:
$Host is an Automatic Variable to the console host object. When you type in a variable name into the Shell window, PowerShell will return the value of that object or properties. Notice the value returned for UI. You should see System.Managmeent.Automation.Internal.Host.InternalHostUserInterface. Now enter the following line:
You should get a response that references the RawUI object. We will now modify the appearance of our SharePoint 2010 Management Shell by changing the properties of the RawUI object. To do this, we will assign the RawUI object to a variable. This will allow us to access this object without having to type the entire name of $Host.UI.RawUI. In order to do this, type the following line:
$SPShell = $Host.UI.RawUI
We can now access this object simply by typing $SPShell in the window.
Notice that we see several items returned. Of these, the ones of interest to us now will be: ForegroundColor, BackgroundColor, and BufferSize.
ForegroundColor will change the color of the text. BackgroundColor will change the color of the background. BuzzerSize contains a height and width properties that will change the size of the screen. The PowerShell window is restricted to the number of characters that fit across the width and height. These values are stored in an object called WindowSize.
We will now modify the fore mentioned colors and the screen size. Enter the following lines:
$SPShell.ForegroundColor = “Blue”
Your SharePoint 2010 Management Shell should look like figure 1.x. From here, we will modify the size of the window and the title.
$size = $SPShell.WindowSize
$size.width = 100
$size.height = 50
$SPShell.BufferSize = $size
$SPShell.WindowTitle = “PowerShell V2 for SharePoint 2010”
Now that you have successfully modified your environment, close it and reopen it. You will see that everything has returned to its original state. This is because the changes you make are not persisted. If you want to modify your environment and keep these changes, you will need to create or modify the PowerShell profile file.
Profiles in PowerShell
Your PowerShell environment is configured with the use of Profile files located throughout the file system. Type the following into your Shell window:
This will return the profile location that applies to the current user, but only the Microsoft.PowerShell. You have three other profile locations to choose from.
- %UserProfile%My DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1
- %UserProfile%My DocumentsWindowsPowerShellprofile.ps1
- %windir%system32WindowsPowerShellv1.0 Microsoft.PowerShell_profile.ps1
The second file listed will only affect the current user, but will be applied to all of the Shells. The second applies to all users on the system, but only the Microsoft.PowerShell. Finally, the fourth location applies to all users and all shells.
We will focus on the first file. You will need to create the first location, as it is not created by default. To do this, simply create a folder named WindowsPowerShell in the My Documents location.
You may now create a text file similar to the one below and save it as Microsoft.PowerShell_profile.
Once this file is saved, you will be able to open up the SharePoint 2010 Management Shell and see the customized environment.