{"id":4950,"date":"2012-09-20T00:01:00","date_gmt":"2012-09-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/20\/use-powershell-redirection-operators-for-script-flexibility\/"},"modified":"2012-09-20T00:01:00","modified_gmt":"2012-09-20T00:01:00","slug":"use-powershell-redirection-operators-for-script-flexibility","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-redirection-operators-for-script-flexibility\/","title":{"rendered":"Use PowerShell Redirection Operators for Script Flexibility"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy Ed Wilson talks about using the new Windows PowerShell redirectioin operators to add flexibility to a script.<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! There is something about Windows PowerShell that I don&rsquo;t get. Normally, I can use the redirection arrows to write to a text file, but sometimes it does not work. Is this a bug in Windows PowerShell, or is there something I don&rsquo;t understand (probably)?<\/p>\n<\/p>\n<p>&mdash;TL<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello TL,<\/p>\n<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I spent much of last night working on our three-week European tour itinerary. We will be visiting a number of Windows PowerShell User Groups, and seeing even more Windows PowerShell MVPs. This morning, the Scripting Wife emailed our working itinerary to our various contacts. Stay tuned. Like <a href=\"https:\/\/www.youtube.com\/watch?v=JmH9ahaTt7k\" target=\"_blank\">The Monkeys<\/a>, we may be coming to your town.<\/p>\n<\/p>\n<h2>Understanding various output streams<\/h2>\n<\/p>\n<p>Understanding output might sound complicated, but it is actually easy. Windows PowerShell has various output streams. What most beginning Windows PowerShell users think of as output is only one output stream&mdash;the success output; but there are many output streams:<\/p>\n<\/p>\n<ul>\n<li>Success output<\/li>\n<li>Error message output<\/li>\n<li>Warning message output<\/li>\n<li>Verbose message output<\/li>\n<li>Debug message output<\/li>\n<\/ul>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;The descriptions of and uses for the various redirection operators are explained in the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847746.aspx\" target=\"_blank\">about_Redirection<\/a> topic in the TechNet Library. Refer to that topic for a great chart that details all of the redirection operators.<\/p>\n<\/p>\n<p>In Windows PowerShell&nbsp;3.0, there are three new redirection operators. These operators are shown here.<\/p>\n<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"127\" valign=\"top\">\n<p><b>&nbsp; Stream<\/b><\/p>\n<\/td>\n<td width=\"108\" valign=\"top\">\n<p><b>&nbsp; Operator<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"127\" valign=\"top\">\n<p>&nbsp; Warning<\/p>\n<\/td>\n<td width=\"108\" valign=\"top\">\n<p>&nbsp; 3&gt;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"127\" valign=\"top\">\n<p>&nbsp; Verbose<\/p>\n<\/td>\n<td width=\"108\" valign=\"top\">\n<p>&nbsp; 4&gt;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"127\" valign=\"top\">\n<p>&nbsp; Debug<\/p>\n<\/td>\n<td width=\"108\" valign=\"top\">\n<p>&nbsp; 5&gt;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Like other redirection operators, there are the overwrite (&gt;) and the append (&gt;&gt;) versions. The default values for the different streams (configured as preference variables) display errors and warnings, but not verbose and debug. This is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir variable: -Include &#8220;*preference&#8221; -Recurse<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">ConfirmPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; High<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">DebugPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SilentlyContinue<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">ErrorActionPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Continue<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">ProgressPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Continue<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">VerbosePreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SilentlyContinue<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">WarningPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Continue<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">WhatIfPreference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; False<\/p>\n<\/p>\n<p>When a default preference variable receives a <b>SilentlyContinue<\/b> value, it means that the stream does not display to the host. Instead, Windows PowerShell silently continues past the command to the next command. When the value is <b>Continue<\/b>, it means the stream displays to the host, and Windows PowerShell continues to the next command.<\/p>\n<\/p>\n<p>To change a value temporarily, I add a value assignment at the top of a Windows PowerShell script. The change affects the host as long as the host remains open. When the host is closed, it reverts to normal. To make the change &ldquo;permanent,&rdquo; add the value to the Windows PowerShell profile.<\/p>\n<\/p>\n<p>In the DemoRedirectionStream.ps1 script, I first set three preference variables as shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">$VerbosePreference = &#8220;continue&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">$WarningPreference = &#8220;continue&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">$DebugPreference = &#8220;continue&#8221;<\/p>\n<\/p>\n<p>Next I use the <b>Write-Warning<\/b>, <b>Write-Verbose<\/b>, and the <b>Write-Debug<\/b> cmdlets to emit text strings through the various output streams. The first <b>Write-Warning<\/b> does not redirect to a text file, and it appears to the Windows PowerShell host. The remaining commands redirect to various text files, and they do not appear in the Windows PowerShell host output. This is shown in the following image.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3312.HSG-9-20-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3312.HSG-9-20-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<\/p>\n<p>The three output text files are shown here.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1411.HSG-9-20-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1411.HSG-9-20-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<\/p>\n<p>TL, that is all there is to using Windows PowerShell&nbsp;3.0 redirection operators. Join me tomorrow as I talk about more Windows PowerShell. It will be cool!<\/p>\n<\/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\" target=\"_blank\">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>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using the new Windows PowerShell redirectioin operators to add flexibility to a script. &nbsp;Hey, Scripting Guy! There is something about Windows PowerShell that I don&rsquo;t get. Normally, I can use the redirection arrows to write to a text file, but sometimes it does not work. Is this [&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":[51,362,3,4,45],"class_list":["post-4950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-powershell-3","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using the new Windows PowerShell redirectioin operators to add flexibility to a script. &nbsp;Hey, Scripting Guy! There is something about Windows PowerShell that I don&rsquo;t get. Normally, I can use the redirection arrows to write to a text file, but sometimes it does not work. Is this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4950","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=4950"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4950\/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=4950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}