{"id":322,"date":"2006-11-22T04:00:00","date_gmt":"2006-11-22T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/11\/22\/write-what\/"},"modified":"2006-11-22T04:00:00","modified_gmt":"2006-11-22T12:00:00","slug":"write-what","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/11\/22\/write-what\/","title":{"rendered":"Write-What?"},"content":{"rendered":"<p><P>Okay&#8230; I hope I&#8217;m not the last person in the world to have figured this out or I&#8217;ll feel dumber than I already do.<\/P><P>I&#8217;ve been messing with PowerShell a lot and couldn&#8217;t figure out what the difference was between write-output and write-host, two cmdlets that both APPEAR to do the same thing. But now I get it.<\/P><P>My first clue was that the alias write points to write-output&#8230; so, although nearly EVERY example you see on the Internet uses write-host to produce output in the shell, it seemed as if write-output, with its convenient write alias, SHOULD be the one we all use.<\/P><P>And it should.<\/P><P>Some background is required: First, understand that the guts of PowerShell are really a big &#8216;old DLL, not an EXE. When you run Powershell.exe, you&#8217;re running what&#8217;s called a <EM>hosting application<\/EM>. In this case, the hosting app does nothing but host what I&#8217;ll call the Shell Engine, which is that big ol&#8217; DLL. However, there are other hosting applications in the universe, such as the Exchange 2007 MMC. Obviously, not all hosting apps are equal: The Exch2007 MMC does lots more than just host the PowerShell Engine, even if that&#8217;s all Powershell.exe really does.<\/P><P>The Write-Output cmdlet (and the Write alias, therefore) spews output that&#8217;s meant to be displayed. In Powershell.exe, that results in text being displayed on the command line; in the Exch2007 MMC something else happens &#8211; perhaps a dialog box is displayed, depending on the circumstances. Remember, the Exch2007 MMC works by hosting PowerShell and calling on PoSH cmdlets to do its work; Write-Output becomes the primary means by which PoSH delivers output to the GUI. For example, Get-Mailbox might &#8220;write&#8221; a bunch of mailbox objects. In Powershell.exe, that results in a text-based list; the GUI, however, might render those objects as a set of icons.<\/P><P>Write-Host, on the other hand, is intended to <EM>write output to the hosting app itself<\/EM>, not necessarily for display to the end user. In the case of Powershell.exe, the hosting app just converts the output to test, so the result looks similar to Write-Output. But Write-Host is intended to <EM>communicate with the hosting app<\/EM>, not to communicate (necesarily) with the app&#8217;s <EM>user<\/EM>. <\/P><P>So: If you&#8217;re writing scripts which will be used entirely from Powershell.exe (e.g., the command-line), there&#8217;s no a ton of difference. However, if your scripts will ever run within a different hosting app, there&#8217;s a difference between what write-host does and what write-output does. For consistency, you should stick with Write-Output (or the Write alias) when you&#8217;re wanting to output something that&#8217;s meant for the user to see. <\/P><P>Here&#8217;s an example: Suppoe your script gets loaded into some future PowerShell Super Debugger. Write-Output might spit output into an &#8220;output&#8221; window, showing you what the user of your script would see; Write-Host might write seperate messages to a &#8220;trace&#8221; window, <EM>communicating with the hosting application<\/EM> to display status messages about your script&#8217;s execution (yes, there&#8217;s a Write-Debug which does this for this particular circumstance&#8230; but this is just an example).<\/P><P>Now, this whole business of &#8220;the shell isn&#8217;t just a command-line&#8221; and &#8220;the shell is hosted within other applications&#8221; is something I haven&#8217;t fully integrated into my own personal worldview &lt;grin&gt; yet, but I&#8217;m working on it&#8230; so if I&#8217;ve got this wrong (or if I&#8217;m right but you have a better example)&#8230; <EM>please<\/EM> drop me a line or post a comment.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p><P>Okay&#8230; I hope I&#8217;m not the last person in the world to have figured this out or I&#8217;ll feel dumber than I already do.<\/P><P>I&#8217;ve been messing with PowerShell a lot and couldn&#8217;t figure out what the difference was between write-output and write-host, two cmdlets that both APPEAR to do the same thing. But now I get it.<\/P><P>My first &#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":[2,25],"tags":[],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/322","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=322"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}