Customize the PowerShell Console for Increased Efficiency

Customize the PowerShell Console for Increased Efficiency

  • Comments 6
  • Likes

Summary: Learn how to easily configure the Windows PowerShell console prompt for more efficiency and increased productivity.

Microsoft Scripting Guy, Ed Wilson, is here. Well, once again it is the weekend. The first ever Pittsburgh PowerShell Users Group meeting was a resounding, sold-out success. The Scripting Wife and I had a great time at the meeting, and we had the chance to see old friends, and to make new friends as well. I predict we will begin to make Pittsburgh one of our frequent haunts—it is a cool town, and the Windows PowerShell Users Group is awesome.

The problem with the PowerShell console

One problem that I have (because I have a dozen computers that I use on a regular basis) is keeping my Windows PowerShell console so that it appears the same. Because I might take screen shots to use in the Hey, Scripting Guy! blog or in a book I might be writing, it is important that the Windows PowerShell console be standardized. In addition, when I make a presentation or teach, the default red error message with the black background does not show up very well with some projectors—and it really does not show up well when printed in black and white.

It is true, that the properties of the Windows PowerShell console are configurable via the application, but that is a manual process, and it is difficult to make it repeatable. The Windows PowerShell console properties box is shown in the image that follows.

Image of dialog box

Tackle the problem of repeatability

In addition to the lack of repeatability, not all of the things that I need to change are available via the Windows PowerShell console properties dialog box. I cannot, for example, configure the color of the error messages. To change items, such as the color of the error messages, requires accessing the Windows PowerShell console settings via Windows PowerShell itself.

For ease of use, I uploaded the Set-PSConsole function to the Scripting Guys Script Repository.

To configure the Windows PowerShell console, I like to access the settings via the $host variable. To get to the user interface portion, I use $host.ui.rawui which contains an instance of an instance of the InternalHostRawUserInterface object.

The foreground color and background color are straightforward value assignments. The two lines of code in my Set-PSConsole function that do this are shown here.

$host.ui.RawUI.ForegroundColor = "black"

$host.ui.RawUI.BackgroundColor = "gray"

Creating a BufferSize object

Next, I need to set the buffer size. The BufferSize property contains an object. The easiest way to obtain access to this object is to query the BufferSize property, and store the resulting object in a variable. It is then a simple matter of assigning new values for the Width and Height properties. When I have modified the properties of the BufferSize object, I need to call the Set_BufferSize method to write the properties back to the Windows PowerShell console. These four lines of code are shown here.

$buffer = $host.ui.RawUI.BufferSize

 $buffer.width = 85

 $buffer.height = 3000

 $host.UI.RawUI.Set_BufferSize($buffer)

Determining the maximum windows size

Now I want to set the window size of the Windows PowerShell console. First, I need to check the maximum window size (based on the current Windows screen resolution). I use the Get_MaxWindowsSize method to obtain the maximum size of the Windows PowerShell console window. I then query the WindowSize property to return a WindowSize object.

Now, I need to perform a few checks. If the width of the maximum window size is greater than or equal to 85, I set the width to 85. If the maximum window size width is less than 85, that is the value I use. I use the same logic for the height. When I have assigned new values to the $ws variable that contains the WindowSize object, I call the Set_WindowSize method to write the values back to the console. These lines of code are shown here.

$maxWS = $host.UI.RawUI.Get_MaxWindowSize()

 $ws = $host.ui.RawUI.WindowSize

 IF($maxws.width -ge 85)

   { $ws.width = 85 }

 ELSE { $ws.width = $maxws.width }

 IF($maxws.height -ge 42)

   { $ws.height = 42 }

 ELSE { $ws.height = $maxws.height }

 $host.ui.RawUI.Set_WindowSize($ws)

Setting the remaining colors

The remaining six lines of code are simple value assignments. I set all of the background colors to white, and choose different foreground colors for the errors, warnings, and verbose messages. Here is the code that sets these six colors.

$host.PrivateData.ErrorBackgroundColor = "white"

 $Host.PrivateData.WarningBackgroundColor = "white"

 $Host.PrivateData.VerboseBackgroundColor = "white"

 $host.PrivateData.ErrorForegroundColor = "red"

 $host.PrivateData.WarningForegroundColor = "DarkGreen"

 $host.PrivateData.VerboseForegroundColor = "DarkBlue"

I add Set-PSConsole to my Windows PowerShell profile, and I call it at the entry point to my profile. This portion of my Windows PowerShell console profile is shown in the image that follows.

Image of profile

The new colors appear in the following image.

Image of profile

I like my new color configuration, but if you don’t, you are free to download Set-PSConsole and create your own color scheme. This is the beauty of Windows PowerShell—if you don’t like something, in general, you can change it.

I hope you have a great weekend. See you tomorrow when I will tackle a problem that I have with my music files and my Zune. The code solved an annoying problem for me—maybe it will help you too. See you then.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed

    I hope that you and Teresa are having a wonderful 4th of advent, too!

    You are saying something very true!

    It's always great to feel comfortable with the tools you are using regulary and having an environment, you are used too, is a good point to start from!

    Great that we can change most aspects of our favorit powershell host by running a quite simple script ... if you know the details.

    Thank you, for presenting these details!

    Klaus.

  • I hadn't thought about setting all that stuff via Powershell.  I'm always tweaking the Command shortcut anyway, so I don't really have much of an issue in hand tweaking my Powershell shortcut either.  And it can be hand to tweak it on the fly as well.

    And you're still going to have to copy over your profile to make this work. :)

  • Hi,

    Is there any posibility to set custom color values of background and font color for entire console using user $profile file ?

  • @Klaus Schulte Teresa and I have been keeping really busy. Had a great visit with the Pittsburg PowerShell Users Group. You are right, one secret to becoming effecient with PowerShell is getting comfortable with the tools, and with having them setup the way one likes to have them. Thank you for your comments.

  • @Bryan Price In general, I was making these settings manually via the the console properties, but eventually it got to the point where I wanted to have them standardized. Many people, have their profile set up so that they can access it on different machines: they keep it on a USB key, they store it on a share, they have it on their Sky Drive, they sync it via Groove ... the possibilities are nearly endless. Thanks for your comment.

  • @KT In Lee Holmes book, there is a script where he modifies various registry values. I adapted that and was planning on using it for an additional article. Unfortunately, those keys do not seem to work on Windows 7 ... have not tested it on downlevel OS's ... So the answer is it might be possible --- unfortunately, I have not found where those keys are documented, to see if they continued on Windows 7 but were moved ...