{"id":1228,"date":"2011-08-05T15:06:10","date_gmt":"2011-08-05T13:06:10","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1228"},"modified":"2015-09-08T20:39:33","modified_gmt":"2015-09-08T18:39:33","slug":"powershell-choose-between-colored-host-text-or-write-to-an-output-file-not-anymore","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/powershell-choose-between-colored-host-text-or-write-to-an-output-file-not-anymore\/","title":{"rendered":"PowerShell &#8211; Choose between colored host text or write to an output file? Not anymore!"},"content":{"rendered":"<p>In PowerShell multiple Write statements are available. The two statements I want to address are Write-Host and Write-Output.\u00a0<\/p>\n<p>What do these two do?<br \/>\n<em>Write-Host<\/em>: Writes customized output to a host<br \/>\n<em>Write-Output<\/em>: Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console\u00a0<\/p>\n<p>An example of Write-Host<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nWrite-Host &quot;Write-Host statement...&quot;\r\n<\/pre>\n<p>The result displayed in the host\u00a0<\/p>\n<p>\u00a0<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/PSWrite0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1230 alignnone\" title=\"PSWrite0\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/PSWrite0.png\" alt=\"\" width=\"199\" height=\"41\" \/><\/a><\/p>\n<p>An example of Write-Output<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nWrite-Output &quot;Write-Output statement...&quot;\r\n<\/pre>\n<p>The result displayed in the host<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/PSWrite00.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1231 alignnone\" title=\"PSWrite00\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/07\/PSWrite00.png\" alt=\"\" width=\"215\" height=\"44\" \/><\/a>\u00a0<\/p>\n<p>At this point there is no difference in the result of these two statements.\u00a0<\/p>\n<h3>Write-Host with ForegroundColor\u00a0<\/h3>\n<p>A nice parameter of Write-Host is -ForegroundColor. Using this parameter with a color, the text displayed in the host has the color specified. I find this very useful to quickly see if a script has\u00a0succeeded or failed. I always display the success message in green, the failed message in red.\u00a0\u00a0\u00a0<\/p>\n<p>To show this the existence of a SPList will be checked. If it doesn\u00b4t exist, it will be created using the following statement:\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$sampleListName = &quot;PS List&quot;\r\n$siteUrl=http:\/\/sp2010dev\r\n$site = Get-SPSite $siteUrl\r\n$web = $site.RootWeb&lt;\/pre&gt;\r\n\r\nfunction CreateList($listName){\r\n  $web.Lists.Add($listName, &quot;Description for list&quot;, &#x5B;Microsoft.SharePoint.SPListTemplateType]::GenericList)\r\n}\r\n\r\nfunction CheckIfListExists(){\r\n$currentList = $web.Lists.TryGetList($sampleListName)\r\n  if($currentList -ne $null){\r\n    Write-Host &quot;List exists&quot; -ForegroundColor Green\r\n  }\r\n  else{\r\n    Write-Host &quot;List doesn't exist, creating now...&quot; -ForegroundColor Red\r\n    CreateList $sampleListName\r\n    Write-Host &quot;Check if list exist...&quot;\r\n    CheckIfListExists\r\n  }\r\n}\r\n\r\nWrite-Host &quot;Calling CheckIfListExists ...&quot;\r\nCheckIfListExists\r\n<\/pre>\n<p>If the list exists a message with green colored text will appear: &#8216;List exists&#8217;, else a message with red colored text will appear: &#8216;List doesn&#8217;t exist, creating now&#8230;&#8217;, followed by the actual\u00a0creation of the list. After this the existence of the list is check again. As you can see if the list creation fails the code will be stuck in an infinite loop, so don\u00b4t use this as production code&#8230;\u00a0\u00a0\u00a0<\/p>\n<p>Outcome when the list doesn\u00b4t exist and is created:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1244\" title=\"PSWrite01\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite01.png\" alt=\"\" width=\"310\" height=\"78\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite01.png 310w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite01-300x75.png 300w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><\/a><\/p>\n<p>Outcome when the list already exists:&lt;picture PSWrite02&gt;<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1245\" title=\"PSWrite02\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite02.png\" alt=\"\" width=\"252\" height=\"34\" \/><\/a><\/p>\n<h3>Write-Output with output to file\u00a0\u00a0\u00a0<\/h3>\n<p>A nice option of Write-Output is the possibility to write the output to a file on the filesystem.\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$file = &quot;C:\\_Tools\\OutputFile.txt&quot;\r\nWrite-Output &quot;Write-Output statement...&quot;\r\nWrite-Output &quot;Write-Output statement...&quot; | Out-File $file\r\n<\/pre>\n<p>The first Write-Output statement writes the output to the host, the second to the file (but not to the host).\u00a0\u00a0\u00a0<\/p>\n<h3>Best of both worlds?\u00a0\u00a0\u00a0<\/h3>\n<p>Ofcourse Write-Host as well as Write-Output has a lot more options to use, but I want to point out something here.<br \/>\nWrite-Host can write colored messages to the host, but it can&#8217;t write output to a file.<br \/>\nWrite-Output can&#8217;t write colored messages to the host, but it can write output to a file.\u00a0\u00a0\u00a0<\/p>\n<p>What if I want to:\u00a0\u00a0\u00a0<\/p>\n<p>write colored messages to the host if I decide to output to the host AND<br \/>\nwrite messages to a file just by using &#8216;| Out-File $file&#8217;<\/p>\n<p>With the current possibilities of both operations I have to choose between:<br \/>\n1. colored messages (and nothing written to the output file) or put the output to a file and host, but no colored messages displayed in the host\u00a0<br \/>\n2. write double statements; Write-Host with -ForeGroundColor to write the colored output to the host and Write-Output to write the output to a file when using &#8216;| Out-File $file&#8217;.\u00a0\u00a0\u00a0<\/p>\n<p>Let me clarify the second option with an example.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nWrite-Output &quot;Write-Output statement...&quot; | Out-File $file\r\nWrite-Host &quot;Write-Host statement...&quot; -ForegroundColor Blue\r\n<\/pre>\n<p>Write-Output writes the message to the file specified in $file and is not written to the host.<br \/>\nWrite-Host writes the message to the host in the color blue.<br \/>\nJust as expected and just want I wanted. But I have to write the messages twice.<\/p>\n<p>Another option:\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nWrite-Output &quot;Write-Output statement...&quot;\r\nWrite-Host &quot;Write-Host statement...&quot; -ForegroundColor Blue\r\n<\/pre>\n<p>The code above is saved in a file and called by:\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\nPS C:\\_Tools&gt; .\\PSScriptFile.ps1\r\n\r\n<\/pre>\n<p>Result: messages appear twice in the host.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite031.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1254\" title=\"PSWrite03\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite031.png\" alt=\"\" width=\"209\" height=\"38\" \/><\/a><\/p>\n<p>Output it to a file:\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\nPS C:\\_Tools&gt; .\\PSScriptFile.ps1 | Out-File $file\r\n\r\n<\/pre>\n<p>Result: No message in the host, but a message in the file!<br \/>\nStill, the messages have to be written twice in the file: Write-Host and Write-Output&#8230;<br \/>\nDo I really have to write all the messages twice to show them colored in the host and write them to an output file?<br \/>\nActually, there is no possibility to make Write-Host write to an output file.<br \/>\nBut the host itself can be accessed to change its text color (and more) and use Write-Output to write to the host (in color!) and to an output file.<\/p>\n<p>The host itself can be accessed by $Host.\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\n$Host.Name\r\n\r\n<\/pre>\n<p>When using PowerGUI Script Editor the results of the above statement is: PowerGUIScriptEditorHost<br \/>\nExecuting the same statement in the console the result is: ConsoleHost<\/p>\n<p>That&#8217;s awesome, $Host knows who he is! \ud83d\ude42<\/p>\n<p>Let&#8217;s try to get the text color of the host:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\n$currentColor = $Host.UI.RawUI.ForegroundColor\r\n\r\n<\/pre>\n<p>PowerGUI Script Editor:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1248\" title=\"PSWrite04\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite04.png\" alt=\"\" width=\"249\" height=\"23\" \/><\/a><\/p>\n<p>Console:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite05.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1249\" title=\"PSWrite05\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite05.png\" alt=\"\" width=\"621\" height=\"45\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite05.png 621w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite05-300x21.png 300w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/a><\/p>\n<p>So what about changing the text color of the host?\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\nfunction WriteCustomOutput($message, &#x5B;System.ConsoleColor]$foregroundcolor)\r\n{\r\n  currentColor = $Host.UI.RawUI.ForegroundColor\r\n  $Host.UI.RawUI.ForegroundColor = $foregroundcolor\r\n  if ($message)\r\n  {\r\n    Write-Output $message\r\n  }\r\n  $Host.UI.RawUI.ForegroundColor = $currentColor\r\n}\r\n\r\nWriteCustomOutput -message &quot;WriteColoredOutput statement...&quot; -foregroundcolor Green\r\n\r\n<\/pre>\n<p>And the result in the host:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1250\" title=\"PSWrite06\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite06.png\" alt=\"\" width=\"271\" height=\"41\" \/><\/a>\u00a0\u00a0\u00a0<\/p>\n<p>Let&#8217;s save the code to a file and call the file from the console:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite07.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1251\" title=\"PSWrite07\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite07.png\" alt=\"\" width=\"321\" height=\"43\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite07.png 321w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite07-300x40.png 300w\" sizes=\"auto, (max-width: 321px) 100vw, 321px\" \/><\/a>\u00a0<\/p>\n<p>And will the output be written to an output file when using the statement:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\r\n.\\PSWriteCustomOutput.ps1 | Out-File OutputFile.txt\r\n\r\n<\/pre>\n<p>Yes it is written to the file:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite08.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1252\" title=\"PSWrite08\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/08\/PSWrite08.png\" alt=\"\" width=\"282\" height=\"77\" \/><\/a>\u00a0\u00a0\u00a0<\/p>\n<h3>Summary<\/h3>\n<p>Write-Host and Write-Output are very simple and useful statements. Sometimes the best of both is wished for.<br \/>\nWith some creativity you can have both. Use WriteCustomOutput!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PowerShell multiple Write statements are available. The two statements I want to address are Write-Host and Write-Output.\u00a0 What do these two do? Write-Host: Writes customized output to a host Write-Output: Sends the specified objects to the next command in &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/powershell-choose-between-colored-host-text-or-write-to-an-output-file-not-anymore\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,8],"tags":[43],"class_list":["post-1228","post","type-post","status-publish","format-standard","hentry","category-powershell","category-sharepoint-2010","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1228"}],"version-history":[{"count":26,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1228\/revisions"}],"predecessor-version":[{"id":1264,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1228\/revisions\/1264"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}