{"id":11761,"date":"2011-12-17T00:01:00","date_gmt":"2011-12-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/12\/17\/customize-the-powershell-console-for-increased-efficiency\/"},"modified":"2011-12-17T00:01:00","modified_gmt":"2011-12-17T00:01:00","slug":"customize-the-powershell-console-for-increased-efficiency","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/customize-the-powershell-console-for-increased-efficiency\/","title":{"rendered":"Customize the PowerShell Console for Increased Efficiency"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to easily configure the Windows PowerShell console prompt for more efficiency and increased productivity.<\/p>\n<p>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&mdash;it is a cool town, and the Windows PowerShell Users Group is awesome.<\/p>\n<h2>The problem with the PowerShell console<\/h2>\n<p>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&mdash;and it really does not show up well when printed in black and white.<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6082.hsg-12-17-11-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6082.hsg-12-17-11-01.png\" alt=\"Image of dialog box\" title=\"Image of dialog box\" \/><\/a><\/p>\n<h2>Tackle the problem of repeatability<\/h2>\n<p>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.<\/p>\n<p>For ease of use, I uploaded the <b>Set-PSConsole<\/b> function to the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Set-PSConsole-function-e49ef714\">Scripting Guys Script Repository<\/a>.<\/p>\n<p>To configure the Windows PowerShell console, I like to access the settings via the <b>$host<\/b> variable. To get to the user interface portion, I use <b>$host.ui.rawui<\/b> which contains an instance of an instance of the <b>InternalHostRawUserInterface <\/b>object.<\/p>\n<p>The foreground color and background color are straightforward value assignments. The two lines of code in my <b>Set-PSConsole<\/b> function that do this are shown here.<\/p>\n<p style=\"padding-left: 30px\">$host.ui.RawUI.ForegroundColor = &#8220;black&#8221;<\/p>\n<p style=\"padding-left: 30px\">$host.ui.RawUI.BackgroundColor = &#8220;gray&#8221;<\/p>\n<h2>Creating a BufferSize object<\/h2>\n<p>Next, I need to set the buffer size. The <b>BufferSize<\/b><i> <\/i>property contains an object. The easiest way to obtain access to this object is to query the <b>BufferSize<\/b><i> <\/i>property, and store the resulting object in a variable. It is then a simple matter of assigning new values for the <b>Width<\/b><i> <\/i>and <b>Height<\/b><i> <\/i>properties. When I have modified the properties of the <b>BufferSize<\/b> object, I need to call the <b>Set_BufferSize<\/b> method to write the properties back to the Windows PowerShell console. These four lines of code are shown here.<\/p>\n<p style=\"padding-left: 30px\">$buffer = $host.ui.RawUI.BufferSize<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$buffer.width = 85<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$buffer.height = 3000<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$host.UI.RawUI.Set_BufferSize($buffer)<\/p>\n<h2>Determining the maximum windows size<\/h2>\n<p>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 <b>Get_MaxWindowsSize<\/b><i> <\/i>method to obtain the maximum size of the Windows PowerShell console window. I then query the <b>WindowSize<\/b><i> <\/i>property to return a <b>WindowSize<\/b> object.<\/p>\n<p>Now, I need to perform a few checks. If the width<i> <\/i>of the maximum window size is greater than or equal to 85, I set the width<i> <\/i>to 85. If the maximum window size width<i> <\/i>is less than 85, that is the value I use. I use the same logic for the height<i>. <\/i>When I have assigned new values to the <b>$ws<\/b> variable that contains the <b>WindowSize<\/b> object, I call the <b>Set_WindowSize<\/b><i> <\/i>method to write the values back to the console. These lines of code are shown here.<\/p>\n<p style=\"padding-left: 30px\">$maxWS = $host.UI.RawUI.Get_MaxWindowSize()<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$ws = $host.ui.RawUI.WindowSize<\/p>\n<p style=\"padding-left: 30px\">&nbsp;IF($maxws.width -ge 85)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; { $ws.width = 85 }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ELSE { $ws.width = $maxws.width }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;IF($maxws.height -ge 42)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; { $ws.height = 42 }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ELSE { $ws.height = $maxws.height }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$host.ui.RawUI.Set_WindowSize($ws)<\/p>\n<h2>Setting the remaining colors<\/h2>\n<p>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.<\/p>\n<p style=\"padding-left: 30px\">$host.PrivateData.ErrorBackgroundColor = &#8220;white&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$Host.PrivateData.WarningBackgroundColor = &#8220;white&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$Host.PrivateData.VerboseBackgroundColor = &#8220;white&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$host.PrivateData.ErrorForegroundColor = &#8220;red&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$host.PrivateData.WarningForegroundColor = &#8220;DarkGreen&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$host.PrivateData.VerboseForegroundColor = &#8220;DarkBlue&#8221;<\/p>\n<p>I add <b>Set-PSConsole<\/b> 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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1018.hsg-12-17-11-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1018.hsg-12-17-11-02.png\" alt=\"Image of profile\" title=\"Image of profile\" \/><\/a><\/p>\n<p>The new colors appear in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7288.hsg-12-17-11-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7288.hsg-12-17-11-03.png\" alt=\"Image of profile\" title=\"Image of profile\" \/><\/a><\/p>\n<p>I like my new color configuration, but if you don&rsquo;t, you are free to <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Set-PSConsole-function-e49ef714\">download Set-PSConsole<\/a> and create your own color scheme. This is the beauty of Windows PowerShell&mdash;if you don&rsquo;t like something, in general, you can change it.<\/p>\n<p>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&mdash;maybe it will help you too. See you then.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[144,3,4,61,45],"class_list":["post-11761","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11761"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11761\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}