{"id":4731,"date":"2009-01-10T18:16:00","date_gmt":"2009-01-10T18:16:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/01\/10\/capture-console-screen\/"},"modified":"2019-02-18T13:12:55","modified_gmt":"2019-02-18T20:12:55","slug":"capture-console-screen","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/capture-console-screen\/","title":{"rendered":"Capture console screen"},"content":{"rendered":"<p><font color=\"#000000\"><font face=\"Verdana\" color=\"#333333\">     <\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\"><font size=\"3\"><font color=\"#000000\"><font face=\"Calibri\">Seeing nicely formatted console examples in a previous post has inspired me to write a script that captures the console screen buffer directly from the Windows PowerShell console host. With this script you can make console &quot;screen shots&quot; to demonstrate some of your favorite Windows PowerShell concepts, and then post them to your blog, or put them on your Web site. <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font color=\"#000000\"><font face=\"Calibri\">How is it done? The console host has a powerful API that is somewhat neglected by most script writers. This API allows you to capture console screen rectangles or output a colorized text in arbitrary areas of the screen. The console API functions are available in Windows PowerShell as properties and methods of the $Host.UI and $Host.UI.RawUI objects. <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font color=\"#000000\"><font face=\"Calibri\">The script makes a good use of one of this methods \u2013 $Host.UI.RawUI.GetBufferContents. This method extracts a rectangular region of the screen buffer. We will use it to capture the console screen buffer contents from the top left corner to the current cursor position. <\/font><\/font><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\"><font size=\"3\"><font color=\"#000000\"><font face=\"Calibri\">Note that $Host.UI and $Host.UI.RawUI are fully supported only on Windows PowerShell console, the one that is started by running powershell.exe. Windows PowerShell ISE does not implement all of these interfaces and therefore cannot be used to run this script. <\/font><\/font><\/font><\/p>\n<p><font size=\"3\"><font color=\"#000000\"><font face=\"Calibri\">To use the script, just open the Windows PowerShell console, run commands that you want to demonstrate, and then run this script to capture the console screen buffer.<\/font><\/font><\/font><\/p>\n<pre style=\"font-size: 10pt;margin: 0in 10pt;line-height: normal\"><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">Windows PowerShell V2                                                                                                  <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">Copyright (C) 2008 Microsoft Corporation. All rights reserved.                                                         <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                       <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">PS C:\\Users\\Vladimir&gt; cd E:\\MyScripts                                                                                  <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">PS E:\\MyScripts&gt; .\\RunMyCoolDemo.ps1                                                                                   <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                       <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">Here are colors which you can use in Windows PowerShell console:                                                       <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                       <\/span><br \/><span style=\"background: #012456;color: black;font-family: courier new\">   Black<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                               <\/span><br \/><span style=\"background: #012456;color: darkblue;font-family: courier new\">   DarkBlue<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                            <\/span><br \/><span style=\"background: #012456;color: darkgreen;font-family: courier new\">   DarkGreen<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                           <\/span><br \/><span style=\"background: #012456;color: darkcyan;font-family: courier new\">   DarkCyan<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                            <\/span><br \/><span style=\"background: #012456;color: darkred;font-family: courier new\">   DarkRed<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                             <\/span><br \/><span style=\"background: #012456;color: #012456;font-family: courier new\">   DarkMagenta<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                         <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">   DarkYellow                                                                                                          <\/span><br \/><span style=\"background: #012456;color: gray;font-family: courier new\">   Gray<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                <\/span><br \/><span style=\"background: #012456;color: darkgray;font-family: courier new\">   DarkGray<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                            <\/span><br \/><span style=\"background: #012456;color: blue;font-family: courier new\">   Blue<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                <\/span><br \/><span style=\"background: #012456;color: green;font-family: courier new\">   Green<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                               <\/span><br \/><span style=\"background: #012456;color: cyan;font-family: courier new\">   Cyan<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                <\/span><br \/><span style=\"background: #012456;color: red;font-family: courier new\">   Red<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                 <\/span><br \/><span style=\"background: #012456;color: magenta;font-family: courier new\">   Magenta<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                             <\/span><br \/><span style=\"background: #012456;color: yellow;font-family: courier new\">   Yellow<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                              <\/span><br \/><span style=\"background: #012456;color: white;font-family: courier new\">   White<\/span><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                               <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                       <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">Note that DarkMagenta and DarkYellow colors are redefined to produce these nice tints of white and blue which are used <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">as default Foreground and Background colors.                                                                           <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">                                                                                                                       <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">PS E:\\MyScripts&gt; $textFileName = &quot;$env:temp\\ConsoleBuffer.txt&quot;                                                         <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">PS E:\\MyScripts&gt; .\\Get-ConsoleAsText | out-file $textFileName -encoding ascii                                          <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">PS E:\\MyScripts&gt; $null = [System.Diagnostics.Process]::Start(&quot;$textFileName&quot;)                                          <\/span><br \/><span style=\"background: #012456;color: #eeedf0;font-family: courier new\">PS E:\\MyScripts&gt;                                                                                                       <\/span><br \/><\/pre>\n<p>    <font size=\"3\"><font color=\"#000000\"><font face=\"Calibri\"><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\">&#160;<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\">And don&#8217;t forget to walk through the code and see how it uses the System.Management.Automation.Host class. You might get some great ideas for your next project. <\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\">&#160;<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\">Enjoy! <\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\">Vladimir Averkin <\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt\">Windows PowerShell Team<\/p>\n<\/p>\n<p>        <\/font><\/font><\/font><\/p>\n<p style=\"font-size: 10pt;margin: 0in 10pt;font-family: lucida console\"><span style=\"color: #006400\">#################################################################################################################<\/span>\n      <br \/><span style=\"color: #006400\"># Get-ConsoleAsText.ps1<\/span>\n      <br \/><span style=\"color: #006400\">#<\/span>\n      <br \/><span style=\"color: #006400\"># The script captures console screen buffer up to the current cursor position and returns it in plain text format.<\/span>\n      <br \/><span style=\"color: #006400\">#<\/span>\n      <br \/><span style=\"color: #006400\"># Returns: ASCII-encoded string.<\/span>\n      <br \/><span style=\"color: #006400\">#<\/span>\n      <br \/><span style=\"color: #006400\"># Example:<\/span>\n      <br \/><span style=\"color: #006400\">#<\/span>\n      <br \/><span style=\"color: #006400\"># $textFileName = &quot;$env:temp\\ConsoleBuffer.txt&quot;<\/span>\n      <br \/><span style=\"color: #006400\"># .\\Get-ConsoleAsText | out-file $textFileName -encoding ascii<\/span>\n      <br \/><span style=\"color: #006400\"># $null = [System.Diagnostics.Process]::Start(&quot;$textFileName&quot;)<\/span>\n      <br \/><span style=\"color: #006400\">#<\/span><\/p>\n<p><span style=\"color: #006400\"># Check the host name and exit if the host is not the Windows PowerShell console host.<\/span>\n      <br \/><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$host<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Name<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">-ne<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8b0000\">&#8216;ConsoleHost&#8217;<\/span><span style=\"color: #000000\">)<\/span>\n      <br \/><span style=\"color: #000000\">{<\/span>\n      <br \/><span style=\"color: #000000\">&#160; <\/span><span style=\"color: #0000ff\">write-host<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #000080\">-ForegroundColor<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8a2be2\">Red<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8b0000\">&quot;This script runs only in the console host. You cannot run this script in $($host.Name).&quot;<\/span>\n      <br \/><span style=\"color: #000000\">&#160; <\/span><span style=\"color: #00008b\">exit<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #800080\">-1<\/span>\n      <br \/><span style=\"color: #000000\">}<\/span><\/p>\n<p><span style=\"color: #006400\"># Initialize string builder.<\/span>\n      <br \/><span style=\"color: #ff4500\">$textBuilder<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #0000ff\">new-object<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8a2be2\">system.text.stringbuilder<\/span><\/p>\n<p><span style=\"color: #006400\"># Grab the console screen buffer contents using the Host console API.<\/span>\n      <br \/><span style=\"color: #ff4500\">$bufferWidth<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$host<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ui<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">rawui<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">BufferSize<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Width<\/span>\n      <br \/><span style=\"color: #ff4500\">$bufferHeight<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$host<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ui<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">rawui<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">CursorPosition<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Y<\/span>\n      <br \/><span style=\"color: #ff4500\">$rec<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #0000ff\">new-object<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8a2be2\">System.Management.Automation.Host.Rectangle<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #800080\">0<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #800080\">0<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$bufferWidth<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">&#8211;<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #800080\">1<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #ff4500\">$bufferHeight<\/span>\n      <br \/><span style=\"color: #ff4500\">$buffer<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$host<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ui<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">rawui<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">GetBufferContents<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$rec<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p><span style=\"color: #006400\"># Iterate through the lines in the console buffer.<\/span>\n      <br \/><span style=\"color: #00008b\">for<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$i<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #800080\">0<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$i<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">-lt<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$bufferHeight<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$i<\/span><span style=\"color: #a9a9a9\">++<\/span><span style=\"color: #000000\">)<\/span>\n      <br \/><span style=\"color: #000000\">{<\/span>\n      <br \/><span style=\"color: #000000\">&#160; <\/span><span style=\"color: #00008b\">for<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$j<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #800080\">0<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$j<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">-lt<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$bufferWidth<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$j<\/span><span style=\"color: #a9a9a9\">++<\/span><span style=\"color: #000000\">)<\/span>\n      <br \/><span style=\"color: #000000\">&#160; <\/span><span style=\"color: #000000\">{<\/span>\n      <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$cell<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$buffer<\/span><span style=\"color: #a9a9a9\">[<\/span><span style=\"color: #ff4500\">$i<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #ff4500\">$j<\/span><span style=\"color: #a9a9a9\">]<\/span>\n      <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$null<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$textBuilder<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Append<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$cell<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Character<\/span><span style=\"color: #000000\">)<\/span>\n      <br \/><span style=\"color: #000000\">&#160; <\/span><span style=\"color: #000000\">}<\/span>\n      <br \/><span style=\"color: #000000\">&#160; <\/span><span style=\"color: #ff4500\">$null<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$textBuilder<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Append<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">&quot;`r`n&quot;<\/span><span style=\"color: #000000\">)<\/span>\n      <br \/><span style=\"color: #000000\">}<\/span><\/p>\n<p><span style=\"color: #00008b\">return<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$textBuilder<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ToString<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">)<\/span>\n      <\/p>\n<p>  <\/font><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seeing nicely formatted console examples in a previous post has inspired me to write a script that captures the console screen buffer directly from the Windows PowerShell console host. With this script you can make console &quot;screen shots&quot; to demonstrate some of your favorite Windows PowerShell concepts, and then post them to your blog, or [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[131,196],"class_list":["post-4731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-console","tag-host"],"acf":[],"blog_post_summary":"<p>Seeing nicely formatted console examples in a previous post has inspired me to write a script that captures the console screen buffer directly from the Windows PowerShell console host. With this script you can make console &quot;screen shots&quot; to demonstrate some of your favorite Windows PowerShell concepts, and then post them to your blog, or [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4731"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4731\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}