{"id":304,"date":"2006-10-30T11:04:38","date_gmt":"2006-10-30T19:04:38","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/10\/30\/funky-times-with-write-host-and-out-host\/"},"modified":"2006-10-30T11:04:38","modified_gmt":"2006-10-30T19:04:38","slug":"funky-times-with-write-host-and-out-host","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/10\/30\/funky-times-with-write-host-and-out-host\/","title":{"rendered":"Funky times with Write-Host and Out-Host"},"content":{"rendered":"<div class=Section1>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>As you know, things aren&#8217;t always what they seem in WindowsPowerShell. Take this short script:<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;PART 1 &#8212;&#8212;&#8212;&#8212;&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;Hello1&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>out-host -input &quot;Hello2&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>write-host &quot;Hello3&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Function TestFunc {<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; &quot;Function1&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; Out-Host -input&quot;Function2&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; Write-Host &quot;Function3&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; Return &quot;Function4&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>}<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;PART 2 &#8212;&#8212;&#8212;&#8212;&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>$out = TestFunc<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;PART 3 &#8212;&#8212;&#8212;&#8212;&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>$out<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>The output of the first part is what you&#8217;d expect:<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>PART 1 &#8212;&#8212;&#8212;&#8212;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Hello1<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Hello2<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Hello3<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>All three of the command shown produce output. Fine.However, the second part calls function TestFunc, and produces the followingoutput:<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>PART 2 &#8212;&#8212;&#8212;&#8212;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Function2<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Function3<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Here, both Out-Host and Write-Host are actually spitting outtext to the console. This occurred <i><span style='font-style:italic'>when thefunction ran; <\/span><\/i>this &#8220;Part 2&#8221; output wasn&#8217;t part ofthe function&#8217;s actual return value. When you look at what was <i><spanstyle='font-style:italic'>returned<\/span><\/i> by the function:<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>PART 3 &#8212;&#8212;&#8212;&#8212;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Function1<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Function4<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>You can see that anything within a function which is just &#8220;output&#8221;(that is, without using a cmdlet) becomes concatenated to the function&#8217;stotal return. This is in addition to anything explicitly returned using theReturn keyword. <o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>So you have to be careful in how you produce output. Withina script, it&#8217;s nearly always safer to produce output by using an outputcmdlet, because within a function any other means won&#8217;t produce <i><spanstyle='font-style:italic'>output, <\/span><\/i>it&#8217;ll produce <i><spanstyle='font-style:italic'>return values<\/span><\/i> from the function. I preferto produce all my return values using the Return keyword. In summary, producingoutput like this:<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&#8220;This is output&#8221;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>That is, not using a cmdlet, is a bad practice (for me, atleast), because it can create inconsistent results depending on where that codeactually occurs. This leaves the question: What&#8217;s the difference betweenOut-Host and Write-Host? I know Write-Host doesn&#8217;t work properly when you&#8217;rehosting PowerShell within another application: Anything produced by Write-Hostproduces a &#8220;function not defined&#8221; (or some such) error which Ihaven&#8217;t wrapped my head around yet. Perhaps perusing the SDK in moredetail will reveal the subtleties there. Stay tuned!<o:p><\/o:p><\/span><\/font><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<div class=Section1>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>As you know, things aren&#8217;t always what they seem in WindowsPowerShell. Take this short script:<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;PART 1 &#8212;&#8212;&#8212;&#8212;&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;Hello1&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>out-host -input &quot;Hello2&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>write-host &quot;Hello3&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>Function TestFunc {<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; &quot;Function1&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; Out-Host -input&quot;Function2&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; Write-Host &quot;Function3&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&nbsp; Return &quot;Function4&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>}<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;PART 2 &#8212;&#8212;&#8212;&#8212;&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>$out = TestFunc<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>&quot;PART 3 &#8212;&#8212;&#8212;&#8212;&quot;<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal style='margin-left:.5in'><font size=2 face=Arial><spanstyle='font-size:10.0pt;font-family:Arial'>$out<o:p><\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'><o:p>&nbsp;<\/o:p><\/span><\/font><\/p>\n<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>The output of the &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}