{"id":65923,"date":"2006-12-04T14:24:00","date_gmt":"2006-12-04T14:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/12\/04\/how-can-i-expand-the-width-of-the-windows-powershell-console\/"},"modified":"2006-12-04T14:24:00","modified_gmt":"2006-12-04T14:24:00","slug":"how-can-i-expand-the-width-of-the-windows-powershell-console","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-expand-the-width-of-the-windows-powershell-console\/","title":{"rendered":"How Can I Expand the Width of the Windows PowerShell Console?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p>Hey, Scripting Guy! How can I expand the width of the Windows PowerShell console? When I display certain kinds of data \u2013 like information from the event logs \u2013 that data gets cut off because the console window isn\u2019t wide enough.<\/p>\n<p>&#8212; TB<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/a><\/p>\n<p>Hey, TB. Wow, what a coincidence: we were just about to ask <i>you<\/i> that very same question! Now what do we do?<\/p>\n<p>No, just kidding: the Scripting Guys never ask anyone for help. Not that we couldn\u2019t use help, mind you; it\u2019s just that, when you work at Microsoft, you quickly learn never to ask anyone anything. <\/p>\n<p>Is that because they might give you the wrong answer? We wish; after all, a wrong answer is at least an answer. No, ask a Microsoft person a question and, instead of giving you an answer, they\u2019ll decide to set up a meeting to talk about the issue. In turn, they\u2019ll go back to their office, notify their manager and your manager (and all the managers\u2019 managers as well as <i>their<\/i> managers) of the question, then invite scores of \u201ckey stakeholders\u201d to attend this suddenly-crucial meeting. <\/p>\n<p>Which, by the way, will often be scheduled for a ridiculous hour like 6:00 PM. (Who the heck wants to be at work at 6:00 PM?) And, of course, they won\u2019t actually <i>schedule<\/i> the meeting; instead, they\u2019ll send everyone a \u201cSched+\u201d, Sched+ being a reference to a Microsoft product (Schedule+) that was last used in 1847. But the memory lingers on.<\/p>\n<table id=\"EKD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Good question: what <i>is<\/i> a key stakeholder? As near as we can tell, if you agree to go to a meeting but then don\u2019t bother to show up you\u2019re a key stakeholder. And because you can\u2019t reach a decision unless the key stakeholders are present, their inevitable absence means that there will <i>always<\/i> be another meeting after this one. Remember the Greek myth of Sisyphus, doomed for eternity to roll a rock up a hill and then, just before he reaches the top, have the rock roll down over him, forcing him to start all over again? Sisyphus has an office just down the hall from the Scripting Guy who writes this column.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>After the meeting has been postponed several times, then moved to several different conference rooms, everyone finally gets together, with the only item on the agenda being this: should we go ahead and schedule another meeting or not? After several such false starts we\u2019ll eventually set up a \u201cvirtual team\u201d that assigns \u201caction items\u201d to the key stakeholders. (We use a lot of quotation marks here at Microsoft, don\u2019t we?) We\u2019ll then spend several months drawing up policies and procedures for how to best address the question. And then, just before we\u2019re ready to come up with an answer, we\u2019ll decide that maybe we don\u2019t really \u201cown\u201d the question after all, and agree that someone else should provide the answer. <\/p>\n<p>And yes, let\u2019s hope that your original question wasn\u2019t \u201cWhere\u2019s the bathroom?\u201d<\/p>\n<p>But things are very different when you ask the <i>Scripting Guys<\/i> a question. Granted, they probably won\u2019t give you an answer, either. But at least there won\u2019t be any paperwork or meetings involved:<\/p>\n<pre class=\"codeSample\">$pshost = get-host\n$pswindow = $pshost.ui.rawui\n$newsize = $pswindow.buffersize\n$newsize.height = 3000\n$newsize.width = 150\n$pswindow.buffersize = $newsize\n$newsize = $pswindow.windowsize\n$newsize.height = 50\n$newsize.width = 150\n$pswindow.windowsize = $newsize\n<\/pre>\n<p>Before we go much further we should note that you don\u2019t <i>have<\/i> to use a script to change the size of the Windows PowerShell console window. Instead, you can always do this:<\/p>\n<table class=\"numberedList\" border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<tbody>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>1.<\/p>\n<\/td>\n<td>\n<p>Start Windows PowerShell.<\/p>\n<\/td>\n<\/tr>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>2.<\/p>\n<\/td>\n<td>\n<p>Right-click the icon in the upper left corner of the title bar and then click <b>Properties<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>3.<\/p>\n<\/td>\n<td>\n<p>On the <b>Layout<\/b> tab set the desired width under the <b>Window Size<\/b> heading<\/p>\n<\/td>\n<\/tr>\n<tr vAlign=\"top\">\n<td class=\"listNumber\" noWrap align=\"right\">\n<p>4.<\/p>\n<\/td>\n<td>\n<p>Click <b>OK<\/b>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>That works just fine. But you\u2019re right: that <i>is<\/i> kind of boring, at least for a scripting column. Therefore, we\u2019ve given you a programmatic way to modify the size of the current Windows PowerShell console. And, as an added bonus, before we\u2019re done we\u2019ll show you how to change the window title and window colors as well.<\/p>\n<p>No thanks necessary; turns out that was an action item that the Scripting Guys, as key stakeholders, agreed to do. <\/p>\n<p>As for the script itself, we start out by creating an object reference (named $pshost) to the Windows PowerShell console object; that\u2019s something we do by calling the <b>Get-Host<\/b> Cmdlet:<\/p>\n<pre class=\"codeSample\">$pshost = get-host\n<\/pre>\n<p>We then use this line of code to create a reference to the console\u2019s <b>UI.RawUI<\/b> child object:<\/p>\n<pre class=\"codeSample\">$pswindow = $pshost.ui.rawui\n<\/pre>\n<p>Now we need to take a brief detour. On our test computer both the Windows PowerShell screen width and the buffer size are set to 120. Does that matter? Yes, it does: you can\u2019t have a screen width that\u2019s bigger than the buffer size. Therefore, before we can increase our window size we need to increase the buffer size. That\u2019s what this block of code does:<\/p>\n<pre class=\"codeSample\">$newsize = $pswindow.buffersize\n$newsize.height = 3000\n$newsize.width = 150\n$pswindow.buffersize = $newsize\n<\/pre>\n<p>It\u2019s a tad bit confusing, but what we\u2019re really doing here is constructing a brand-new buffer size in memory, then applying the properties of that virtual buffer to the actual console window. To do that we first assign the <b>BufferSize<\/b> properties to a variable named $newsize:<\/p>\n<pre class=\"codeSample\">$newsize = $pswindow.buffersize\n<\/pre>\n<p>After we\u2019ve done that we set the values of the buffer size <b>Height<\/b> (3000 lines, the default value) and <b>Width <\/b>(150 characters). We then use this line of code to assign these values to the actual console window:<\/p>\n<pre class=\"codeSample\">$pswindow.buffersize = $newsize\n<\/pre>\n<p>With the buffer size adjusted we then repeat the process with the <b>WindowSize<\/b> property, giving our new window a Height of 50 lines and a Width of 150 characters:<\/p>\n<pre class=\"codeSample\">$newsize = $pswindow.windowsize\n$newsize.height = 50\n$newsize.width = 150\n$pswindow.windowsize = $newsize\n<\/pre>\n<p>Run the script and your Windows PowerShell window will instantly resize itself. (Although we should note that the effect is temporary: the window will revert to its previous size should you close this session and restart it. To permanently change the size of the window you can, among other things, use the UI to change the window properties or put this script in your PowerShell profile.)<\/p>\n<p>Now, as promised, here\u2019s some bonus code that will change the title of the console window (to <i>My PowerShell Session<\/i>) as well as the foreground and background colors (yellow text on a black background). We won\u2019t discuss the whys and wherefores of the bonus code, but you can probably figure it out on your own (hint: see <a href=\"http:\/\/null\/technet\/scriptcenter\/topics\/msh\/output.mspx\"><b>this article<\/b><\/a> for more information on the colors available to you when configuring the PowerShell UI):<\/p>\n<pre class=\"codeSample\">$pshost = get-host\n$pswindow = $pshost.ui.rawui\n$pswindow.windowtitle = \"My PowerShell Session\"\n$pswindow.foregroundcolor = \"Yellow\"\n$pswindow.backgroundcolor = \"Black\"\ncls\n<\/pre>\n<p>See, what did we tell you? With the Scripting Guys you not only get the answer to your question, but you also get answers to questions you didn\u2019t even ask!<\/p>\n<p>Of course, in all fairness, we should point out that the Scripting Guys tend to exaggerate a little when it comes to discussing life here at Microsoft. Needless to say, questions <i>do<\/i> get answered around here, all the time.<\/p>\n<p>Or at least that\u2019s what they told us at our last virtual meeting, where we agreed \u2013 sometime after the holidays, of course \u2013 to get together and try to decide whether or not we should try to make decisions in the future. <\/p>\n<p>Well, not this year, of course, but during the next fiscal year.<\/p>\n<p>We\u2019ll keep you posted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I expand the width of the Windows PowerShell console? When I display certain kinds of data \u2013 like information from the event logs \u2013 that data gets cut off because the console window isn\u2019t wide enough. &#8212; TB Hey, TB. Wow, what a coincidence: we were just about to ask [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,45],"class_list":["post-65923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I expand the width of the Windows PowerShell console? When I display certain kinds of data \u2013 like information from the event logs \u2013 that data gets cut off because the console window isn\u2019t wide enough. &#8212; TB Hey, TB. Wow, what a coincidence: we were just about to ask [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65923","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65923"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65923\/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=65923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}