{"id":134,"date":"2010-03-05T20:10:44","date_gmt":"2010-03-05T09:10:44","guid":{"rendered":"http:\/\/chuchuva.com\/pavel\/?p=134"},"modified":"2016-01-12T07:37:54","modified_gmt":"2016-01-11T20:37:54","slug":"how-to-redirect-output-of-console-program-to-a-file-in-powershell","status":"publish","type":"post","link":"https:\/\/chuchuva.com\/pavel\/2010\/03\/how-to-redirect-output-of-console-program-to-a-file-in-powershell\/","title":{"rendered":"How to redirect output of console program to a file in PowerShell"},"content":{"rendered":"<p>At some stage I decided I need backup plan for my virtual server. After some intensive web surfing I found brilliant command line tool &#8211; <a href=\"http:\/\/s3.codeplex.com\/\">s3.exe<\/a>. The next step was to schedule to run it periodically, every night, when everybody is sleeping.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"140\" data-permalink=\"https:\/\/chuchuva.com\/pavel\/2010\/03\/how-to-redirect-output-of-console-program-to-a-file-in-powershell\/night-mystery\/\" data-orig-file=\"https:\/\/i0.wp.com\/chuchuva.com\/pavel\/images\/2010\/03\/night-mystery.jpg?fit=500%2C375&amp;ssl=1\" data-orig-size=\"500,375\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"night-mystery\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/chuchuva.com\/pavel\/images\/2010\/03\/night-mystery.jpg?fit=500%2C375&amp;ssl=1\" class=\"alignnone size-full wp-image-140\" title=\"\" src=\"https:\/\/i0.wp.com\/chuchuva.com\/pavel\/images\/2010\/03\/night-mystery.jpg?resize=500%2C375\" alt=\"Night\" width=\"500\" height=\"375\" srcset=\"https:\/\/i0.wp.com\/chuchuva.com\/pavel\/images\/2010\/03\/night-mystery.jpg?w=500&amp;ssl=1 500w, https:\/\/i0.wp.com\/chuchuva.com\/pavel\/images\/2010\/03\/night-mystery.jpg?resize=300%2C225&amp;ssl=1 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>I decided to use PowerShell. It&#8217;s new, it&#8217;s powerful, it comes pre-installed with Windows Server 2008. PowerShell is very popular among system administrators.<\/p>\n<p>Also, I got an idea to save output of backup application to log file so that I could see results later. And this is where things didn&#8217;t work as I expected. Here&#8217;s how to redirect console program output to a file:<\/p>\n<pre><code>s3.exe &gt; log.txt<\/code><\/pre>\n<p>If you run this you will be surprised to find that log.txt is empty. As it turns out s3.exe, being a good Windows citizen, writes errors to STDERR stream. To redirect error output to the same file you need to add magic string 2&gt;&amp;1:<\/p>\n<pre><code>s3.exe 2&gt;&amp;1 &gt; log.txt<\/code><\/pre>\n<p>Two new problems appear. First, errors appear as exceptions in log file:<\/p>\n<pre><code>s3.exe : s3.exe version 1.6 - check for updates at http:\/\/s3.codeplex.com\r\nAt PS Logging.ps1:4 char:43\r\n+ s3.exe &lt;&lt;&lt;&lt;  2&gt;&amp;1 &gt; log.txt\r\n    + CategoryInfo          : NotSpecified: (s3.exe version ...s3.codeplex.com\r\n   :String) [], RemoteException\r\n    + FullyQualifiedErrorId : NativeCommandError<\/code><\/pre>\n<p>PowerShell helpfully wraps STDERR output <a href=\"http:\/\/halr9000.com\/article\/837\">into exceptions<\/a>. This is nice but I don&#8217;t need it! All I want is to see how a program worked by examining log file. Do this to unwrap exceptions and show plain text:<\/p>\n<pre><code>s3.exe 2&gt;&amp;1 | foreach-object {$_.ToString()} | Out-File log.txt<\/code><\/pre>\n<p>And second, errors are\u00a0out of order with normal output. They could appear before or after normal lines emitted by a program, making it hard to diagnose problem. To confirm that, I created a simple console program:<\/p>\n<pre><code class=\"prettyprint\">static void Main(string[] args)\r\n{\r\n   for (int i = 0; i &lt; 10; i++)\r\n   {\r\n      Console.WriteLine(\"Hello\");\r\n   }\r\n   Console.Error.WriteLine(\"Bah bah\");\r\n   for (int i = 0; i &lt; 10; i++)\r\n   {\r\n      Console.WriteLine(\"2\");\r\n   }\r\n}\r\n<\/code><\/pre>\n<p>If you run this program from PowerShell and redirect output to a file, error message (bah bah) could appear anywhere in the log file:<\/p>\n<pre><code>Hello\r\n<strong>Bah bah<\/strong>\r\nHello\r\nHello\r\nHello\r\nHello\r\nHello\r\nHello\r\nHello\r\nHello\r\nHello\r\n2\r\n2\r\n2\r\n2\r\n2\r\n2\r\n2\r\n2\r\n2\r\n2<\/code><\/pre>\n<p>The solution I came up with is to let old reliable cmd.exe to do the work:<\/p>\n<pre><code>cmd \/c s3.exe `&gt;log.txt 2`&gt;`&amp;1<\/code><\/pre>\n<p>Note backsticks &#8211; I use them to prevent PowerShell from parsing redirect operators.<\/p>\n<h4>Links<\/h4>\n<p><a href=\"http:\/\/devcentral.f5.com\/weblogs\/Joe\/archive\/2009\/01\/09\/powershell-abcs---o-is-for-output.aspx\">PowerShell ABC&#8217;s &#8211; O is for Output<\/a><br \/>\n<a href=\"http:\/\/groups.google.com.au\/group\/microsoft.public.windows.powershell\/browse_thread\/thread\/41c84d2b76785329\/228e5ee428392294\">How to capture exe output to a PowerShell variable<\/a><br \/>\nAnother approach to unwrap exceptions: <a href=\"http:\/\/knowbody.livejournal.com\/11406.html\">use add-content cmdlet<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some stage I decided I need backup plan for my virtual server. After some intensive web surfing I found brilliant command line tool &#8211; s3.exe. The next step was to schedule to run it periodically, every night, when everybody is sleeping. I decided to use PowerShell. It&#8217;s new, it&#8217;s powerful, it comes pre-installed with &hellip; <a href=\"https:\/\/chuchuva.com\/pavel\/2010\/03\/how-to-redirect-output-of-console-program-to-a-file-in-powershell\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to redirect output of console program to a file in PowerShell<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[10],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-software","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":0,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}