Take Command of Server 2008 with Windows PowerShell – Part 3

Welcome to part three of taking command of Server 2008 with Windows PowerShell!

You’ve made it this far, so I’m assuming you enjoyed part one, where I showed you how to install and access PowerShell, and part two, where I explained PowerShell Cmdlets and PowerShell Providers.

If you missed those two articles, I would suggest reading them first — just to make sure you’ve got all the basics. Now let’s get started with an exercise in customizing your Windows PowerShell.

Customizing Windows PowerShell

Windows PowerShell starts with the same look and feel of the Command Prompt, by default. Let’s make our first exercise of PowerShell power be to customize the PowerShell console.

The steps below will walk you through customizing the console options then saving those settings to be used every time PowerShell is launched.

1. Open Windows PowerShell

2. Create a variable to the PowerShell RawUI object with the command below:

  $psui = (get-host).UI.RawUI  

3. Use the $psui variable to change the background and text colors with the commands below:

  $psui.WindowTitle = “PowerShell PowerStation”  $psui.BackGroundColor = “Blue”  $psui.ForeGroundColor = “Yellow”  

Note: The PowerShell console supports 16 colors. They are; Black, White, Blue, Cyan, Gray, Green, Magenta, Red, Yellow, DarkBlue, DarkCyan, DarkGray, DarkGreen, DarkMagenta, DarkRed and DarkYellow.

4. Use the cls command to clear the console and fill the console with the background color specified

5. View the other properties available to the object by entering $psui

Note: If you wish to change RawUI objects with multiple properties such as the BufferSize property you must assign the property to another variable to modify the object’s properties like the example below:

  $psbs = $psui.BufferSize  $psbs.Height = 3000  $psbs.Width = 120  

6. Decide on how you want to customize the PowerShell console with the properties available

7. Open your PowerShell Profile with notepad with the command below:

  notepad $profile  

8. Assuming that you are running Server 2008 and this is the first time creating a PowerShell profile, Notepad should display an error that the file was not found.

Click Ok to close the error and return to PowerShell to rectify the problem.

9. Try using the command below to create the profile file:

  create-item $profile –type “file”  

10. The previous command attempts to create the profile file under your ‘DocumuentsWindows PowerShell’ directory, but with no Windows PowerShell directory, no file was created

11. Use some PowerShell power to create the directory then the profile file by entering the commands below:

  new-item $profile.SubString(0, $profile.LastIndexOfAny(“\”))  create-item $profile –type “file”  

Note: If you have any experience with the .NET framework the first command of this step should look familiar. It utilizes the methods of the string object to parse the profile path out of the profile variable.

12. Open the newly created file by entering notepad $profile again

13. Enter the property changes as you would in PowerShell into the profile file. Below is an example of the contents of a profile file with the same setting examples in these steps:

  $psui = (get-host).UI.RawUI  $psbs = $psui.BufferSize  $psui.WindowTitle = “PowerShell PowerStation”  $psui.BackGroundColor = “Blue”  $psui.ForeGroundColor = “Yellow”  $psbs.Height = 3000  $psbs.Width = 120  cls  

14. Click File | Save to save the changes to the profile

If script execution is enabled, when you launch PowerShell it will automatically start with the configuration set in the profile file. If not enabled, proceed to the next section for information on enabling it.

This example only covers customizing the PowerShell console window. Keep in mind that any variables or functions you define in the profile file will be automatically available for use when you launch PowerShell.

This is it for today! Next week we’ll go into PowerShell scripting — so don’t miss the last article of taking command of Server 2008 with Windows PowerShell. See you next week!